﻿Imports System
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Printing
Imports System.Windows.Forms
Imports System.ComponentModel
Imports System.Collections
Imports System.Threading
Imports System.Security.Cryptography
Imports System.Reflection
Imports System.Type

Imports Security

Imports System.Diagnostics

Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst.swUserPreferenceStringValue_e
Imports SolidWorks.Interop.swconst

Public Class frmmain
    Dim FilesList() As String

    '--------------------------------------------------------------------------------------------------
    '未注册限制
    Private Sub UnReg()
        If IsReg = False Then
            Do Until ListView1.Items.Count <= 5
                ListView1.Items(ListView1.Items.Count - 1).Remove()
            Loop

            Do Until TreeView1.Nodes.Count <= 3
                TreeView1.Nodes(TreeView1.Nodes.Count - 1).Remove()
            Loop
        End If
    End Sub


    '释放窗口
    Private Sub fmmain_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        End
    End Sub

    '关闭窗口
    Private Sub frmmain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        Dim Ini As New Ini
        Ini.INIWrite(Ini_File, "SwTool", "DoModel", DoModel)
        Ini.INIWrite(Ini_File, "SwTool", "GetNO", IsGetNo)

        '写最近的文件
        DelFile(RF_File, FileIO.RecycleOption.DeletePermanently)
        For Each LI In ListBox1.Items
            WirteLineString(RF_File, LI.ToString)
        Next

        '写收藏夹
        DelFile(Favor_File, FileIO.RecycleOption.DeletePermanently)
        For Each amenustrip As ToolStripMenuItem In Menu_Favor.DropDownItems
            If IsDirectoryExists(amenustrip.Text.ToString) Then
                WirteLineString(Favor_File, amenustrip.Text)
            End If
        Next
    End Sub

    '加载主窗口
    Private Sub fmmain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        On Error Resume Next
        Dim Ini As New Ini
        Dim HardDiskNumber As String
        Dim KeyFile As String
        Dim KeyText(1) As String
        Dim Security As New Security.Security
        Dim PhysicalDrive As New Security.PhysicalDrive
        Dim LocalFolder As String

        Me.Hide()

        ToolStripMenuItem3.Visible = False

        Dim fs As New frmSplashScreen
        fs.Show()
        fs.Refresh()
        System.Threading.Thread.Sleep(1200)  '间隔的时间毫秒


        LocalFolder = My.Application.Info.DirectoryPath

        Microsoft.Win32.Registry.SetValue("HKEY_LOCAL_MACHINE\SOFTWARE\leaky", "AppPath", LocalFolder)

        Ini_File = LocalFolder & "\SwTool.ini"
        RF_File = LocalFolder & "\RF.ini"
        Favor_File = LocalFolder & "\Favor.ini"

        SwVersion = Ini.INIRead(Ini_File, "SwTool", "SwVersion")


        ReNameColor = Color.PaleTurquoise
        ReferencedColor = Color.PaleVioletRed
        ReplaceColor = Color.Gold
        Review = CheckState.Unchecked

        KeyFile = My.Application.Info.DirectoryPath & "\SwTool.key"

        HardDiskNumber = PhysicalDrive.GetHDDID(0)

        KeyText(0) = Security.GetKey(UCase(Security.MD5(HardDiskNumber, 32)))

        If IsFileExsts(KeyFile) = True Then KeyText(1) = My.Computer.FileSystem.ReadAllText(KeyFile)

        If KeyText(0) = KeyText(1) Then
            IsReg = True
            Me.Text = AppTitle
        Else
            IsReg = False
            Me.Text = AppTitle & " (未注册)"
            MsgBox("未注册版支持5个工程图和3个零件! 注册请查看帮助::在线       ", MsgBoxStyle.OkOnly & MsgBoxStyle.Exclamation)
        End If

        Menu_Reg.Visible = IsReg Xor True

        SplitContainer1.SplitterDistance = Me.Width / 2

        ListView1.Columns.Add("序号", 50, HorizontalAlignment.Center)
        ListView1.Columns.Add("文件", ListView1.Width \ 2, HorizontalAlignment.Left)
        ListView1.Columns.Add("图号", ListView1.Width \ 2, HorizontalAlignment.Left)

        ListView2.Columns.Add("属性", 100, HorizontalAlignment.Left)
        ListView2.Columns.Add("读取值", (ListView2.Width - 100) \ 2, HorizontalAlignment.Left)
        ListView2.Columns.Add("修改值", (ListView2.Width - 100) \ 2, HorizontalAlignment.Left)


        '检查更新
        AutoUpdate = Ini.INIRead(Ini_File, "SwTool", "AutoUpdate")
        If AutoUpdate = 1 Then
            Menu_Update_Click(sender, e)
        End If

        Part_Number = Ini.INIRead(Ini_File, "SwTool", "Part_Number")

        DoModel = Ini.INIRead(Ini_File, "SwTool", "DoModel")            '"SLDDRW"

        CAD_To = Ini.INIRead(Ini_File, "SwTool", "CAD_To")
        CAD_Add_Part_NO = Ini.INIRead(Ini_File, "SwTool", "CAD_Add_Part_NO")
        Compear = Ini.INIRead(Ini_File, "SwTool", " Compear")

        IsGetNo = Ini.INIRead(Ini_File, "SwTool", "GetNo")
        Select Case IsGetNo
            Case 1
                Menu_GetNO.CheckState = CheckState.Checked
            Case 0
                Menu_GetNO.CheckState = CheckState.Unchecked
            Case Else
                IsGetNo = 0
                Menu_GetNO.CheckState = CheckState.Unchecked
        End Select

        '加载自定义属性文件
        Custom_Properties_File = Ini.INIRead(Ini_File, "SwTool", "Custom_Properties_File")

        If IsFileExsts(Custom_Properties_File) = False Then
            'SwVersion = 2001
            'Do
            '    SwVersion = SwVersion + 1
            '    Custom_Properties_File = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\SolidWorks\SolidWorks " & SwVersion & "\ExtReferences", "Custom Property Folders", "") & "\properties.txt"
            'Loop Until IsFileExsts(Custom_Properties_File) = True


            Dim swApp As SldWorks
            swApp = CreateObject("SldWorks.Application")
            Custom_Properties_File = swApp.GetUserPreferenceStringValue(swFileLocationsCustomPropertyFile) & "\properties.txt"
            Ini.INIWrite(Ini_File, "SwTool", "Custom_Properties_File", Custom_Properties_File)
        End If

        '获取自定义属性

        'Get_Custom_Properties()
        CustomPropertyNames = File.ReadAllLines(Custom_Properties_File, System.Text.Encoding.Default)

        '加载最近的文件夹
        ListBox1.Items.Clear()
        If IsFileExsts(RF_File) = True Then
            'ReadTextLinesFromFileToListBox(RF_File, ListBox1)
            For Each s In File.ReadAllLines(RF_File, System.Text.Encoding.Default)
                ListBox1.Items.Add(s)
            Next
            ReBuildRecentFolder()
        Else
            For i = 0 To Menu_Recent_Folder.DropDownItems.Count - 1
                Menu_Recent_Folder.DropDownItems.Item(i).Visible = False
            Next
        End If
        ListBox1.Items.Clear()

        '加载收藏夹
        Dim doItem As ToolStripMenuItem = MenuStrip1.Items("Menu_Favor")
        For Each s In File.ReadAllLines(Favor_File, System.Text.Encoding.Default)
            '检查是否重复
            For Each amenustrip As ToolStripMenuItem In Menu_Favor.DropDownItems
                If s = amenustrip.Text.ToString Then
                    GoTo 998
                End If
            Next

            If IsDirectoryExists(s) Then
                doItem.DropDownItems.Add(s, Nothing, AddressOf menuItem_Click).Name = s
                'doItem.DropDownItems.Add(s, Nothing, AddressOf menuItem_Click).Name = s
            End If
998:
        Next

        If (DoModel <> DRW And DoModel <> PRT) Then DoModel = DRW

        If DoModel = DRW Then
            Menu_Part_Mode.Text = "模型"
            Menu_Part_Mode.Image = ToolStripMenuItem2.Image

            ToolStripSplitButton_Domode.Text = "工程图"
            ToolStripSplitButton_Domode.Image = ToolStripMenuItem1.Image
        ElseIf DoModel = PRT Then
            SplitContainer2.SplitterDistance = SplitContainer2.Height * 0.8
            Menu_Part_Mode.Text = "工程图"
            Menu_Part_Mode.Image = ToolStripMenuItem1.Image

            ToolStripSplitButton_Domode.Text = "模型"
            ToolStripSplitButton_Domode.Image = ToolStripMenuItem2.Image
        End If

        Set_DoModel()
        'Fill_LV2(ListView2)
        Set_LV_Color(ListView2, Color.BlanchedAlmond, Color.White)

        ListView1.Dock = DockStyle.Fill
        TreeView1.Dock = DockStyle.Fill
        SplitContainer2.Dock = DockStyle.Fill
        SplitContainer3.Dock = DockStyle.Fill


        IsTopMost = False

        fs.Close()

        With Me
            .Width = 1000
            .Height = 500
            .Location = New Point((Screen.PrimaryScreen.WorkingArea.Width - Me.Width) / 2, (Screen.PrimaryScreen.WorkingArea.Bottom - Me.Height) / 2)
            .Show()
            .TopMost = IsTopMost
        End With


    End Sub

    '退出
    Private Sub Menu_Exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_Exit.Click
        '写收藏夹
        For Each amenustrip As ToolStripMenuItem In Menu_Favor.DropDownItems
            If IsDirectoryExists(amenustrip.Text.ToString) Then
                WirteLineString(Favor_File, amenustrip.Text)
            End If
        Next
        Me.Dispose()
    End Sub


    '窗体大小改变
    Private Sub frmmain_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
        With SplitContainer1
            .Top = 62
            .Left = 30
            .Width = Me.Width - 45
            .Height = Me.Height - 115
        End With
    End Sub

    '菜单--批量修改属性
    Private Sub Menu_Custom_Properties_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_Custom_Properties.Click
        On Error Resume Next

        Dim LVI As ListViewItem
        Dim AttribList As ListViewItem
        Dim i As Integer

        Me.Cursor = Cursors.WaitCursor
        If ListView1.CheckedItems.Count > 0 Then
            With ToolStripProgressBar1
                .Minimum = 0
                .Maximum = ListView1.CheckedItems.Count
                .Value = 0
            End With

            For Each LVI In ListView1.CheckedItems

                '添加任务代码
                '--------------------------------
                Application.DoEvents()
                A_SLDDRWFile.Slddrw_File_Name = LVI.SubItems(1).Text

                If IsFileExsts(A_SLDDRWFile.Slddrw_File_Name) = True Then

                    ToolStripStatusLabel1.Text = "进程文件：" & A_SLDDRWFile.Slddrw_File_Name

                    '获取自定义属性
                    A_SLDDRWFile = GetCustomPropertys(A_SLDDRWFile, True)

                    ListView2.BeginUpdate()
                    ListView2.Items.Clear()

                    '添加到 Listview2
                    For i = 0 To A_SLDDRWFile.CustomPropertyCount
                        Dim LV As New ListViewItem(A_SLDDRWFile.CustomProperty(i).Name)
                        LV.SubItems.Add(A_SLDDRWFile.CustomProperty(i).Evaluated)
                        LV.SubItems.Add(A_SLDDRWFile.CustomProperty(i).Evaluated)
                        ListView2.Items.Add(LV)
                    Next

                    For Each AttribList In ListView4.Items
                        Select Case AttribList.Text
                            Case "批量修改"
                                Dim IsHere As Boolean = False
                                For i = 0 To A_SLDDRWFile.CustomPropertyCount
                                    If AttribList.SubItems(1).Text = A_SLDDRWFile.CustomProperty(i).Name Then   '属性名称存在修改
                                        If A_SLDDRWFile.CustomProperty(i).LinkedTo = "" Then     '不是有链接的值
                                            A_SLDDRWFile.CustomProperty(i).Value = AttribList.SubItems(3).Text
                                            'ListView2.Items(i).SubItems(2).Text = A_SLDDRWFile.CustomProperty(i).Value
                                            IsHere = True
                                            Exit For
                                        End If
                                    End If
                                Next

                                If IsHere = True Then
                                    SetCustomProperty(A_SLDDRWFile.Slddrw_File_Name, A_SLDDRWFile.CustomProperty(i).Name, A_SLDDRWFile.CustomProperty(i).Value, True)
                                ElseIf IsHere = False Then    '属性不存在新增
                                    AddCustomProperty(A_SLDDRWFile.Slddrw_File_Name, AttribList.SubItems(2).Text.ToString, AttribList.SubItems(3).Text.ToString, True)
                                    Dim LV As New ListViewItem(AttribList.SubItems(1).Text)
                                    LV.SubItems.Add("")
                                    LV.SubItems.Add(AttribList.SubItems(2).Text)
                                    ListView2.Items.Add(LV)
                                End If

                            Case "批量删除"
                                For i = 0 To A_SLDDRWFile.CustomPropertyCount
                                    If AttribList.SubItems(1).Text = A_SLDDRWFile.CustomProperty(i).Name Then   '属性名称相同
                                        DeleteCustomProperty(A_SLDDRWFile.Slddrw_File_Name, A_SLDDRWFile.CustomProperty(i).Name, True)
                                        'ListView2.Items(i).Remove()
                                        'i = i - 1
                                        Exit For
                                    End If
                                Next

                            Case "批量新增"
                                Dim IsHere As Boolean = False
                                For i = 0 To A_SLDDRWFile.CustomPropertyCount
                                    If AttribList.SubItems(1).Text = A_SLDDRWFile.CustomProperty(i).Name Then   '属性名称存在跳过
                                        IsHere = True
                                        Exit For
                                    End If
                                Next

                                If IsHere = False Then    '属性不存在新增
                                    AddCustomProperty(A_SLDDRWFile.Slddrw_File_Name, AttribList.SubItems(1).Text, AttribList.SubItems(3).Text, True)
                                    Dim LV As New ListViewItem(AttribList.SubItems(1).Text)
                                    LV.SubItems.Add(AttribList.SubItems(2).Text)
                                    LV.SubItems.Add("")
                                    ListView2.Items.Add(LV)
                                End If

                            Case "批量替换"
                                For i = 0 To A_SLDDRWFile.CustomPropertyCount
                                    If AttribList.SubItems(1).Text = A_SLDDRWFile.CustomProperty(i).Name Then   '属性名称相同
                                        If A_SLDDRWFile.CustomProperty(i).LinkedTo = "" Then     '不是有链接的值
                                            A_SLDDRWFile.CustomProperty(i).Value = Replace(A_SLDDRWFile.CustomProperty(i).Value, AttribList.SubItems(2).Text.ToString, AttribList.SubItems(3).Text.ToString)
                                            'ListView2.Items(i).SubItems(2).Text = A_SLDDRWFile.CustomProperty(i).Value
                                            SetCustomProperty(A_SLDDRWFile.Slddrw_File_Name, A_SLDDRWFile.CustomProperty(i).Name, A_SLDDRWFile.CustomProperty(i).Value, True)
                                            Exit For
                                        End If
                                    End If
                                Next
                        End Select
                    Next

                    'Set_LV_Color(ListView2, Color.BlanchedAlmond, Color.White)
                    ListView2.EndUpdate()
                End If
                '----------------------------------
                ToolStripProgressBar1.Value = ToolStripProgressBar1.Value + 1
                ToolStripStatusLabel2.Text = Val(ToolStripProgressBar1.Value.ToString) * 100 \ ToolStripProgressBar1.Maximum & "%"

            Next

        End If

        Dim SldDrwFile As SLDDRW_File = Nothing
        If ListView1.SelectedItems.Count > 0 Then
            SldDrwFile.Slddrw_File_Name = ListView1.SelectedItems(0).SubItems(1).Text
        End If

        ReFreshCustomProperty(SldDrwFile, ListView2)

        ListView4.Items.Clear()
        ToolStripStatusLabel1.Text = "修改自定义属性完成"
        Me.Cursor = Cursors.Default
        MsgBox("修改自定义属性完成", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "自定义属性")
    End Sub

    '菜单--添加文件
    Private Sub Menu_Add_File_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_Add_File.Click
        'On Error Resume Next

        Dim FileFullName As String
        Dim Nod As TreeNode
        Dim KeyIndex As Integer
        Dim NodeIconText As NodeIconText

        With OpenFileDialog1
            .Multiselect = True
            If DoModel = DRW Then
                .Title = "添加SLDDRW文件"
                .Filter = "SW工程图(*.SLDDRW)|*.SLDDRW"
            ElseIf DoModel = PRT Then
                .Title = "添加SLDASM\SLDPRT文件"
                .Filter = "SW模型(*.SLDASM;*.SLDPRT)|*.SLDASM;*.SLDPRT|SW装配(*.SLDASM)|*.SLDASM;|SW零件(*.SLDPRT)|*.SLDPRT"
            End If
            If .ShowDialog = DialogResult.OK Then
                FilesList = .FileNames
            Else
                Exit Sub
            End If
        End With

        ToolStripStatusLabel1.Text = "添加文件"
        Application.DoEvents()
        WaitStart()

        If DoModel = DRW Then

            'ListView1.Items.Clear()
            ListView1.BeginUpdate()

            For Each FileFullName In FilesList
                If IsTempFile(FileFullName) = False Then
                    If InListView(ListView1, FileFullName) = -1 Then
                        Dim LVI As ListViewItem = ListView1.Items.Add(ListView1.Items.Count + 1)
                        LVI.SubItems.Add(FileFullName)
                        If IsGetNo = 1 Then
                            LVI.SubItems.Add(GetCustomProperty(FileFullName, Part_Number, True))
                        Else
                            LVI.SubItems.Add("")
                        End If
                    End If
                End If
            Next

            UnReg()
            ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)
            ListView1.Columns(0).Width = 40
            If ListView1.Columns(1).Width < (ListView1.Width - 100) Then
                ListView1.Columns(1).Width = ListView1.Width - 100
            End If

            ReBuiltNumber(ListView1)
            Set_LV_Color(ListView1, Color.LightBlue, Color.White)
            ListView1.EndUpdate()

            '------------------------------------------------------------------------------
        ElseIf DoModel = PRT Then

            'TreeView1.Nodes.Clear()
            'TreeView1.BeginUpdate()

            ToolStripProgressBar1.Minimum = 0
            ToolStripProgressBar1.Maximum = FilesList.Length
            ToolStripProgressBar1.Value = 0

            For Each FileFullName In FilesList

                If IsTempFile(FileFullName) = True Then GoTo 999

                NodeIconText = GetImageIndex(FileFullName, True)
                If NodeIconText.IconIndex > 3 Then GoTo 999

                If IsInTreeView(TreeView1, FileFullName) = False Then
                    Nod = TreeView1.Nodes.Add("", GetOnlyname(FileFullName), NodeIconText.IconIndex, NodeIconText.IconIndex)
                    KeyIndex = Nod.Index
                    Nod.ToolTipText = FileFullName

                    Get_Depend(Nod, FileFullName)
                End If
999:
                ToolStripProgressBar1.Value = ToolStripProgressBar1.Value + 1
            Next
            UnReg()
            'TreeView1.EndUpdate()

        End If

        WaitEnd()
        ToolStripStatusLabel1.Text = "添加文件完成"

    End Sub

    '工具栏--添加文件
    Private Sub ToolStripButton_File_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton_File.Click
        Menu_Add_File_Click(sender, e)
    End Sub

    '菜单--添加文件夹
    Private Sub Menu_Add_Folder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_Add_Folder.Click
        Dim FolderFullName As String

        With FolderBrowserDialog1
            .Description = "添加SLDDRW/SLDPRT/SLDASM文件"
            .ShowNewFolderButton = False
            .RootFolder = System.Environment.SpecialFolder.Desktop
            Dim result As DialogResult = .ShowDialog()
            If result = DialogResult.OK Then
                FolderFullName = .SelectedPath
            Else
                Exit Sub
            End If
        End With

        ToolStripStatusLabel1.Text = "扫描文件夹：" & FolderFullName
        Application.DoEvents()
        WaitStart()

        '扫描文件夹
        If DoModel = DRW Then

            'ListView1.Items.Clear()

            ListView1.BeginUpdate()

            TransFiles(FolderFullName, "", ListView1)

            UnReg()

            ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)
            ListView1.Columns(0).Width = 40
            If ListView1.Columns(1).Width < (ListView1.Width - 100) Then
                ListView1.Columns(1).Width = ListView1.Width - 100
            End If

            ReBuiltNumber(ListView1)
            Set_LV_Color(ListView1, Color.LightBlue, Color.White)

            ListView1.EndUpdate()
            '-------------------------------------------------------------------------------------
        ElseIf DoModel = PRT Then
            'TreeView1.Nodes.Clear()
            'TreeView1.BeginUpdate()


            Select Case MsgBox("是否扫描工程图?", MsgBoxStyle.Question + MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton2, "扫描文件夹")
                Case vbYes
                    TransFiles(FolderFullName, DRW, TreeView1)
                Case vbNo
                    TransFiles(FolderFullName, PRT, TreeView1)
            End Select



            UnReg()
            'TreeView1.EndUpdate()
        End If

        ListBox1.Items.Insert(0, FolderFullName)
        If ListBox1.Items.Count > 5 Then
            ListBox1.Items.RemoveAt(5)
        End If

        ReBuildRecentFolder()

        WaitEnd()

        ToolStripStatusLabel1.Text = "扫描文件夹完成"
    End Sub

    '工具栏--添加文件夹
    Private Sub ToolStripButton_Folder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton_Folder.Click
        Menu_Add_Folder_Click(sender, e)
    End Sub

    '菜单--打开文件
    Private Sub Menu_Open_File_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_Open_File.Click
        'Dim LVI As ListViewItem
        Dim FileFullName As String = Nothing
        If DoModel = DRW Then
            If ListView1.Items.Count > 0 Then
                'For Each LVI In ListView1.SelectedItems
                'FileFullName = LVI.SubItems(1).Text
                'Next
                FileFullName = ListView1.SelectedItems(0).SubItems(1).Text.ToString
            End If
        ElseIf DoModel = PRT Then
            If TreeView1.Nodes.Count > 0 Then
                FileFullName = TreeView1.SelectedNode.ToolTipText
            End If
        End If

        If IsFileExsts(FileFullName) Then
            System.Diagnostics.Process.Start(FileFullName)
        Else
            MsgBox(FileFullName & "打开错误", MsgBoxStyle.OkOnly & MsgBoxStyle.Critical)
        End If
    End Sub

    '文件列表菜单--打开文件
    Private Sub TSMenu_Open_File_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMenu_Open_File.Click
        Menu_Open_File_Click(sender, e)
    End Sub

    '菜单--打开所有文件
    Private Sub Menu_Open_Files_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_Open_Files.Click
        Dim FileFullName As String = Nothing
        If DoModel = DRW Then
            If ListView1.Items.Count > 0 Then
                For Each LVI As ListViewItem In ListView1.CheckedItems
                    FileFullName = LVI.SubItems(1).Text
                    If IsFileExsts(FileFullName) Then
                        System.Diagnostics.Process.Start(FileFullName)
                    End If
                Next
            End If
        ElseIf DoModel = PRT Then
            If TreeView1.Nodes.Count > 0 Then
                FileFullName = TreeView1.SelectedNode.ToolTipText
                If IsFileExsts(FileFullName) Then
                    System.Diagnostics.Process.Start(FileFullName)
                End If
            End If
        End If


    End Sub

    '菜单--清除列表
    Private Sub Menu_Clear_List_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_Clear_List.Click
        ListView1.Items.Clear()
        ListView2.Items.Clear()
        ListView3.Items.Clear()
        ListView4.Items.Clear()
        TreeView1.Nodes.Clear()

        'Fill_LV2(ListView2)
        'Set_LV_Color(ListView2, Color.BlanchedAlmond, Color.White)

        'WebBrowser1.Navigate(Taobao_SW2007)

    End Sub

    '菜单--移除文件
    Private Sub Menu_ReMove_List_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_ReMove_List.Click
        If ListView1.SelectedItems.Count > 0 And DoModel = DRW Then
            For Each LVI As ListViewItem In ListView1.SelectedItems
                ListView1.Items.Remove(LVI)
            Next

            'ListView1.SelectedItems(0).Remove()

            ReBuiltNumber(ListView1)
            Set_LV_Color(ListView1, Color.LightBlue, Color.White)

        ElseIf DoModel = PRT And TreeView1.Nodes.Count > 0 Then
            If TreeView1.SelectedNode.Parent Is Nothing Then
                TreeView1.SelectedNode.Remove()
            End If
        End If
    End Sub

    '菜单--全选
    Private Sub Menu_All_Select_List_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_All_Select_List.Click
        For Each LVI As ListViewItem In ListView1.Items
            LVI.Checked = True
        Next
    End Sub

    '工具栏--全选
    Private Sub ToolStripButton_Checks_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton_Checks.Click
        Menu_All_Select_List_Click(sender, e)
    End Sub

    '菜单--反选
    Private Sub Menu_Un_Select_List_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_Un_Select_List.Click
        For Each LVI As ListViewItem In ListView1.Items
            LVI.Checked = LVI.Checked Xor True
        Next
    End Sub

    '工具栏--反选
    Private Sub ToolStripButton_ReCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton_ReCheck.Click
        Menu_Un_Select_List_Click(sender, e)
    End Sub

    '工具栏--取消选择
    Private Sub ToolStripButton_Checks_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton_UnChecks.Click
        For Each LV As ListViewItem In ListView1.CheckedItems
            LV.Checked = False
        Next
    End Sub

    '文件列表排序
    Private Sub ListView1_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles ListView1.ColumnClick
        If e.Column <> 0 Then


            For Each colh As ColumnHeader In ListView1.Columns
                colh.Text = colh.Text.Replace(" ▼", "").Replace(" ▲", "")
            Next

            Dim columnsort As New ColumnSort(e.Column)

            columnsort.bAscending = (ListView1.Sorting = SortOrder.Ascending)

            If columnsort.bAscending Then
                ListView1.Columns(e.Column).Text = ListView1.Columns(e.Column).Text & " ▲"
                ListView1.Sorting = SortOrder.Descending
            Else
                ListView1.Columns(e.Column).Text = ListView1.Columns(e.Column).Text & " ▼"
                ListView1.Sorting = SortOrder.Ascending
            End If
            ListView1.ListViewItemSorter = columnsort
            ListView1.ListViewItemSorter = Nothing

            ReBuiltNumber(ListView1)
            Set_LV_Color(ListView1, Color.LightBlue, Color.White)
        End If

    End Sub

    '拖入文件到文件列表
    Private Sub ListView1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragEnter
        ' 检查自定义的 DataFormat ListViewItem 数组。
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effect = DragDropEffects.All
        End If

    End Sub

    '菜单--另存为cad
    Private Sub Menu_Save_As_CAD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_Save_As_CAD.Click
        Dim LVI As ListViewItem

        If ListView1.CheckedItems.Count > 0 Then
            With ToolStripProgressBar1
                .Minimum = 0
                .Maximum = ListView1.CheckedItems.Count
                .Value = 0
            End With


            If CAD_To = 1 Then
                With FolderBrowserDialog1
                    .Description = "另存为CAD到文件夹"
                    .ShowNewFolderButton = True
                    .RootFolder = System.Environment.SpecialFolder.Desktop
                    Dim result As DialogResult = .ShowDialog()
                    If result = DialogResult.OK Then
                        A_SLDDRWFile.Parent_Folder = .SelectedPath
                    Else
                        ToolStripStatusLabel1.Text = "另存为CAD完成"
                        Me.Cursor = Cursors.Default
                        Exit Sub
                    End If
                End With
            Else
                A_SLDDRWFile.Parent_Folder = ""
            End If

            Me.Cursor = Cursors.WaitCursor
            For Each LVI In ListView1.CheckedItems
                'FList.Selected = True

                '添加任务代码
                '--------------------------------
                Application.DoEvents()
                A_SLDDRWFile.Slddrw_File_Name = LVI.SubItems(1).Text

                If IsFileExsts(A_SLDDRWFile.Slddrw_File_Name) = True Then

                    ToolStripStatusLabel1.Text = "进程文件：" & A_SLDDRWFile.Slddrw_File_Name

                    'GetCustomPropertys()

                    Save_as_Dwg(A_SLDDRWFile.Parent_Folder, A_SLDDRWFile)
                End If

                '----------------------------------

                ToolStripProgressBar1.Value = ToolStripProgressBar1.Value + 1
                ToolStripStatusLabel2.Text = Val(ToolStripProgressBar1.Value.ToString) * 100 \ ToolStripProgressBar1.Maximum & "%"

            Next
        End If
        ToolStripStatusLabel1.Text = "另存为CAD完成"
        Me.Cursor = Cursors.Default
        MsgBox("另存为CAD完成", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "CAD")
    End Sub

    '菜单--打印
    Private Sub Menu_Print_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_Print.Click
        Dim PrinterName As String
        Dim LVI As ListViewItem
        Dim Copies As Integer

        With PrintDialog1
            If DialogResult.OK = .ShowDialog() Then
                PrinterName = .PrinterSettings.PrinterName
                Copies = .PrinterSettings.Copies
            Else
                Exit Sub
            End If
        End With

        Me.Cursor = Cursors.WaitCursor
        If ListView1.CheckedItems.Count > 0 Then
            With ToolStripProgressBar1
                .Minimum = 0
                .Maximum = ListView1.CheckedItems.Count
                .Value = 0
            End With

            For Each LVI In ListView1.CheckedItems
                '添加任务代码
                '--------------------------------
                Application.DoEvents()
                A_SLDDRWFile.Slddrw_File_Name = LVI.SubItems(1).Text

                If IsFileExsts(A_SLDDRWFile.Slddrw_File_Name) = True Then
                    ToolStripStatusLabel1.Text = "进程文件：" & A_SLDDRWFile.Slddrw_File_Name

                    PrintDrawing(A_SLDDRWFile.Slddrw_File_Name, PrinterName, Copies)
                End If

                '----------------------------------

                ToolStripProgressBar1.Value = ToolStripProgressBar1.Value + 1
                ToolStripStatusLabel2.Text = Val(ToolStripProgressBar1.Value.ToString) * 100 \ ToolStripProgressBar1.Maximum & "%"

            Next

        End If
        ToolStripStatusLabel1.Text = "打印完成"
        PrintDocument1.Dispose()
        Me.Cursor = Cursors.Default
        MsgBox("打印完成", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "打印")
    End Sub

    '工具栏--打印
    Private Sub ToolStripButton_Print_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton_Print.Click
        Menu_Print_Click(sender, e)
    End Sub

    '工具栏--修改属性
    Private Sub ToolStripButton_Property_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton_Property.Click
        Menu_Custom_Properties_Click(sender, e)
    End Sub

    '工具栏--另存为cad
    Private Sub ToolStripButton_Save_As_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton_Save_As.Click
        Menu_Save_As_CAD_Click(sender, e)
    End Sub

    '在文件列表中选择了一个文件
    Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged

        If ListView1.Items.Count = 0 Or ListView1.SelectedItems.Count = 0 Then
            Exit Sub
        End If

        If SelecdItem Is Nothing = False Then
            SelecdItem.BackColor = SelecdColor
        End If


        '缓存背景颜色
        SelecdItem = ListView1.SelectedItems(0)
        SelecdColor = ListView1.SelectedItems(0).BackColor
        ListView1.SelectedItems(0).BackColor = Color.LightCoral

        A_SLDDRWFile.Slddrw_File_Name = ListView1.SelectedItems(0).SubItems(1).Text

        If IsFileExsts(A_SLDDRWFile.Slddrw_File_Name) = False Then
            MsgBox("文件 " & A_SLDDRWFile.Slddrw_File_Name & " 不存在，将被移除列表。", MsgBoxStyle.Critical, "错误")
            ListView1.SelectedItems(0).Remove()
            Set_LV_Color(ListView1, Color.LightBlue, Color.White)
            Exit Sub
        End If

        A_SLDDRWFile = ReFreshCustomProperty(A_SLDDRWFile, ListView2)

        If A_SLDDRWFile.CustomPropertyCount > 0 Then
            For Each a In A_SLDDRWFile.CustomProperty
                If a.Name = Part_Number Then
                    ListView1.SelectedItems(0).SubItems(2).Text = a.Value
                    Exit For
                End If
            Next
        Else
            ListView1.SelectedItems(0).SubItems(2).Text = ""
        End If

        ToolStripStatusLabel1.Text = ListView1.SelectedItems(0).Index + 1 & "#" & A_SLDDRWFile.Slddrw_File_Name

        A_SLDDRWFile.Part_Name = GetOnlyname(A_SLDDRWFile.Slddrw_File_Name)
        A_SLDDRWFile.Parent_Folder = My.Computer.FileSystem.GetFileInfo(A_SLDDRWFile.Slddrw_File_Name).DirectoryName

        A_SLDDRWFile.Sldprt_File_Name = A_SLDDRWFile.Parent_Folder & "\" & A_SLDDRWFile.Part_Name & PRT
        If IsFileExsts(A_SLDDRWFile.Sldprt_File_Name) = True Then
            ToolStripStatusLabel2.Text = "存在关联的零件文件"
            Exit Sub
        End If

        A_SLDDRWFile.Sldprt_File_Name = A_SLDDRWFile.Parent_Folder & "\" & A_SLDDRWFile.Part_Name & ASM
        If IsFileExsts(A_SLDDRWFile.Sldprt_File_Name) = True Then
            ToolStripStatusLabel2.Text = "存在关联的装配文件"
            Exit Sub
        End If

        ToolStripStatusLabel2.Text = "未找到关联的模型文件"

    End Sub

    '菜单--关于
    Private Sub Menu_About_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_About.Click
        If IsTopMost = True Then
            Me.TopMost = False
        End If
        frmAbout.ShowDialog()
    End Sub

    '菜单--设置
    Private Sub Menu_Option_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_Option.Click
        If IsTopMost = True Then
            Me.TopMost = False
        End If
        frmoption.ShowDialog()
    End Sub

    '工具栏--切换到模型
    Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
        DoModel = DRW

        ToolStripSplitButton_Domode.Text = "工程图"
        ToolStripSplitButton_Domode.Image = ToolStripMenuItem1.Image

        Menu_Part_Mode.Text = "模型"
        Menu_Part_Mode.Image = ToolStripMenuItem2.Image

        'Menu_Clear_List_Click(sender, e)
        Set_DoModel()
    End Sub

    '工具栏--切换到工程图
    Private Sub ToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem2.Click
        DoModel = PRT
        ToolStripSplitButton_Domode.Text = "模型"
        ToolStripSplitButton_Domode.Image = ToolStripMenuItem2.Image

        Menu_Part_Mode.Text = "工程图"
        Menu_Part_Mode.Image = ToolStripMenuItem1.Image

        'Menu_Clear_List_Click(sender, e)
        Set_DoModel()
    End Sub

    '菜单--切换2种模式
    Private Sub Menu_Part_Mode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_Part_Mode.Click
        If DoModel = DRW Then
            DoModel = PRT                   '转到模型

            Menu_Part_Mode.Text = "工程图"
            Menu_Part_Mode.Image = ToolStripMenuItem1.Image
            ToolStripSplitButton_Domode.Text = "模型"
            ToolStripSplitButton_Domode.Image = ToolStripMenuItem2.Image
        ElseIf DoModel = PRT Then
            DoModel = DRW       '转到工程图

            Menu_Part_Mode.Text = "模型"
            Menu_Part_Mode.Image = ToolStripMenuItem2.Image
            ToolStripSplitButton_Domode.Text = "工程图"
            ToolStripSplitButton_Domode.Image = ToolStripMenuItem1.Image
        End If
        'Menu_Clear_List_Click(sender, e)
        Set_DoModel()

    End Sub

    '工具栏--窗口在前
    Private Sub ToolStripButton_TopMost_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ToolStripButton_TopMost.CheckedChanged
        Select Case ToolStripButton_TopMost.CheckState
            Case CheckState.Checked
                IsTopMost = True
            Case CheckState.Indeterminate

            Case CheckState.Unchecked
                IsTopMost = False
        End Select

        Me.TopMost = IsTopMost

    End Sub

    '树中选择了文件
    Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
        On Error Resume Next
        A_SLDDRWFile.Sldprt_File_Name = e.Node.ToolTipText
        ToolStripStatusLabel1.Text = A_SLDDRWFile.Sldprt_File_Name
        If IsFileExsts(ChangeFileExe(A_SLDDRWFile.Sldprt_File_Name, DRW)) = False Then
            ToolStripStatusLabel2.Text = "未找到对应的工程图文件"
        Else
            ToolStripStatusLabel2.Text = "存在对应的工程图文件"
        End If

        If IsFileExsts(A_SLDDRWFile.Sldprt_File_Name) = True And Review = CheckState.Checked Then
            '    AxEModelViewControl1.OpenDoc(A_SLDDRWFile.Sldprt_File_Name, False, False, True, "")
            '    AxEModelViewControl1.ViewOperator = EModelView.EMVOperators.eMVOperatorZoom
            '    If GetFileExtension(A_SLDDRWFile.Sldprt_File_Name) = DRW Then
            '        AxEModelViewControl1.ViewOrientation = EModelView.EMVViewOrientation.eMVOrientationFront
            '    Else
            '        AxEModelViewControl1.ViewOrientation = EModelView.EMVViewOrientation.eMVOrientationIsoMetric
            '    End If


            Dim edrawings_File = My.Computer.FileSystem.SpecialDirectories.Temp & "\edrawings_file.html"

            'edrawings_File = "file:///" & edrawings_File
            'edrawings_File = Replace(edrawings_File, "\", "/")
            'edrawings_File = Replace(edrawings_File, " ", "%20")

            CreatHtml(edrawings_File, A_SLDDRWFile.Sldprt_File_Name)
            WebBrowser1.Navigate(edrawings_File)
        End If
    End Sub

    '菜单---注册
    Private Sub Menu_Reg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_Reg.Click
        If IsTopMost = True Then
            Me.TopMost = False
        End If
        frmreg.ShowDialog()
    End Sub


    '开始进度条
    Private Sub WaitStart()
        Me.Cursor = Cursors.WaitCursor   '光标为沙漏
        Me.DoubleBuffered = True
        Control.CheckForIllegalCrossThreadCalls = False
        With ToolStripProgressBar1
            .MarqueeAnimationSpeed = 100
            .Maximum = 100
            .Minimum = 0
            .Value = 0
            .Style = ProgressBarStyle.Marquee
        End With

        BackgroundWorker1.RunWorkerAsync()  '开启多线程显示进度条

    End Sub

    '完成进度条
    Private Sub WaitEnd()
        BackgroundWorker1.WorkerSupportsCancellation = True
        BackgroundWorker1.CancelAsync()  '停止多线程
        With ToolStripProgressBar1
            .MarqueeAnimationSpeed = 0
            .Style = ProgressBarStyle.Blocks
            .Value = .Maximum
        End With
        Me.Cursor = Cursors.Default   '光标为箭头
    End Sub

    '后台
    Private Sub BackgroundWorker1_DoWork1(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim worker As System.ComponentModel.BackgroundWorker = CType(sender, System.ComponentModel.BackgroundWorker)
        ShowProgress(worker)
    End Sub

    '显示进度条
    Private Sub ShowProgress(ByVal worker As System.ComponentModel.BackgroundWorker)
        On Error Resume Next
        While Not worker.CancellationPending
            If ToolStripProgressBar1.Value >= 100 Then
                ToolStripProgressBar1.Value = 0
            Else
                ToolStripProgressBar1.Value = ToolStripProgressBar1.Value + 10
            End If
            ' xx millisecond delay
            System.Threading.Thread.Sleep(200)
        End While
        ToolStripProgressBar1.MarqueeAnimationSpeed = 0

    End Sub

    '拖文件到树
    Private Sub TreeView1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TreeView1.DragEnter
        ' 检查自定义的 DataFormat ListViewItem 数组。
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effect = DragDropEffects.All
        End If
    End Sub

    'Private Sub TreeView1_NodeMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick
    'Dim NewFileName As String
    'Select e.Button
    '    Case Windows.Forms.MouseButtons.Left           '双击左键修改文件名
    '        If e.Node.ImageIndex < 2 Then
    '            NewFileName = InputBox("新文件名：", "更改文件名", e.Node.Text)

    '            If NewFileName = "" Or NewFileName = e.Node.Text Then Exit Sub

    '            WaitStart()

    '            ChangeReferencedFile(TreeView1, e.Node, ListBox1, NewFileName)

    '            WaitEnd()

    '        ElseIf e.Node.ImageIndex = 2 Then
    '            With OpenFileDialog1
    '                .Title = "替换SLDASM|SLDPRT文件"
    '                .FileName = e.Node.Text
    '                .Filter = "SW装配(*.SLDASM)|*.SLDASM;|SW零件(*.SLDPRT)|*.SLDPRT|SW模型(*.SLDASM;*.SLDPRT)|*.SLDASM;*.SLDPRT"
    '                .Multiselect = False
    '                Dim result As DialogResult = .ShowDialog()
    '                If result = DialogResult.OK Then
    '                    ChangeOneReferencedFile(TreeView1, e.Node, ListBox1, .FileName)
    '                Else
    '                    Exit Sub
    '                End If
    '            End With

    '        End If
    '    Case Windows.Forms.MouseButtons.Right

    'End Select

    'End Sub


    '工具栏--更改文件名
    Private Sub ToolStripButton_ReName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton_ReName.Click
        TSMenu_Rename_Click(sender, e)
    End Sub

    '工具栏--重载树
    Private Sub ToolStripButton_Refresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton_Refresh.Click
        Dim Node As TreeNode

        For Each Node In TreeView1.Nodes
            Node.Nodes.Clear()
            Get_Depend(Node, Node.ToolTipText)
        Next

        ToolStripStatusLabel2.Text = "重载关系完成"
    End Sub

    '切换模式后界面设定
    Private Sub Set_DoModel()
        Dim Is_DRW As Boolean
        Dim Is_PRT As Boolean

        If DoModel = DRW Then '工程图模式为 true
            Is_DRW = True
        Else
            Is_DRW = False
        End If

        Is_PRT = Is_DRW Xor True

        ListView1.Visible = Is_DRW
        SplitContainer3.Visible = Is_DRW
        TreeView1.Visible = Is_PRT
        SplitContainer2.Visible = Is_PRT

        Menu_Task.Enabled = Is_DRW

        ToolStripButton_Property.Enabled = Is_DRW
        ToolStripButton_Save_As.Enabled = Is_DRW
        ToolStripButton_Print.Enabled = Is_DRW
        ToolStripButton_ReName.Enabled = Is_PRT
        ToolStripButton_Refresh.Enabled = Is_PRT
        ToolStripButton_Rrepair.Enabled = Is_PRT

        TSMenu_Print_File.Visible = Is_DRW
        TSMenu_Saveas_CAD.Visible = Is_DRW

        TSMenu_Rename.Visible = Is_PRT

        TSMenu_CopyAttrib.Visible = Is_DRW
        TSMenu_PasteAttrib.Visible = Is_DRW

        TSMenu_Replace.Visible = Is_PRT
        TSMenu_File_AutoRepair.Visible = Is_PRT
        ToolStripSeparator4.Visible = Is_PRT
        TSMenu_CollapseAll_Nodes.Visible = Is_PRT
        TSMenu_ExpandAll_Nodes.Visible = Is_PRT
        TSMenu_File_Review.Visible = Is_PRT
    End Sub

    '菜单--在线
    Private Sub Menu_OnLine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_OnLine.Click
        Try
            Process.Start(Taobao)
        Catch ex As Exception
            ' The error message
            'MessageBox.Show("Unable to open link that was clicked.")
        End Try

    End Sub

    '树菜单--重命名
    Private Sub TSMenu_Rename_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMenu_Rename.Click
        Dim NewFileName As String
        Dim Node As TreeNode

        If TreeView1.Nodes.Count = 0 Then Exit Sub

        Node = TreeView1.SelectedNode

        If IsTopMost = True Then
            Me.TopMost = False
        End If

        NewFileName = Trim(InputBox("新文件名：", "更改文件名", Node.Text))

        If IsTopMost = True Then
            TopMost = True
        End If

        If NewFileName = "" Or NewFileName = Node.Text Then Exit Sub

        WaitStart()
        ListView3.BeginUpdate()

        ChangeReferencedFile(TreeView1, Node, ListView3, NewFileName)

        ListView3.EndUpdate()
        WaitEnd()

    End Sub

    '文件列表菜单--属性
    Private Sub TSMenu_File_Attrib_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMenu_File_Attrib.Click
        Dim LVI As ListViewItem

        If DoModel = DRW Then
            If ListView1.Items.Count > 0 Then
                For Each LVI In ListView1.SelectedItems
                    ShowPropertie(Me.Handle, LVI.SubItems(1).Text)
                Next
            End If
        ElseIf DoModel = PRT Then
            If TreeView1.Nodes.Count > 0 Then
                ShowPropertie(Me.Handle, TreeView1.SelectedNode.ToolTipText)
            End If
        End If

    End Sub

    '拖入文件到listview
    Private Sub ListView1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragDrop
        Dim FilesList() As String
        Dim i As Integer
        Dim FileFullName As String

        ToolStripStatusLabel1.Text = "扫描文件/文件夹"
        Application.DoEvents()
        WaitStart()
        ListView1.BeginUpdate()

        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            FilesList = e.Data.GetData(DataFormats.FileDrop)
            For i = 0 To UBound(FilesList)
                FileFullName = FilesList(i)


                If IsDirectoryExists(FileFullName) = True Then

                    TransFiles(FileFullName, DRW, ListView1)

                ElseIf IsFileExsts(FileFullName) = True And GetFileExtension(FileFullName) = DRW Then
                    If InListView(ListView1, FileFullName) = -1 Then

                        Dim LVI As ListViewItem = ListView1.Items.Add(ListView1.Items.Count + 1)
                        LVI.SubItems.Add(FileFullName)
                        If IsGetNo = 1 Then
                            LVI.SubItems.Add(GetCustomProperty(FileFullName, Part_Number, True))
                        Else
                            LVI.SubItems.Add("")
                        End If

                    End If

                End If
            Next
        End If

        UnReg()

        ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)
        ListView1.Columns(0).Width = 40
        If ListView1.Columns(1).Width < (ListView1.Width - 100) Then
            ListView1.Columns(1).Width = ListView1.Width - 100
        End If

        ReBuiltNumber(ListView1)
        Set_LV_Color(ListView1, Color.LightBlue, Color.White)
        ListView1.EndUpdate()
        ToolStripStatusLabel1.Text = "扫描文件/文件夹完成"
        WaitEnd()

    End Sub

    '拖入文件（夹）到树
    Private Sub TreeView1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TreeView1.DragDrop
        Dim Files_Folders() As String
        Dim Nit As NodeIconText
        Dim FileFullName As String
        Dim Nod As TreeNode
        Dim KeyIndex As Short

        Dim swApp As SldWorks

        swApp = CreateObject("SldWorks.Application")

        ToolStripStatusLabel1.Text = "扫描文件/文件夹"
        Application.DoEvents()
        'TreeView1.Nodes.Clear()
        TreeView1.BeginUpdate()
        WaitStart()

        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            Files_Folders = e.Data.GetData(DataFormats.FileDrop)

            For Each FileFullName In Files_Folders

                If IsDirectoryExists(FileFullName) = True Then
                    '文件夹
                    Select Case MsgBox("是否扫描工程图?", MsgBoxStyle.Question + MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton2, "扫描文件夹")
                        Case vbYes
                            TransFiles(FileFullName, DRW, TreeView1)
                        Case vbNo
                            TransFiles(FileFullName, PRT, TreeView1)
                    End Select
                ElseIf IsFileExsts(FileFullName) = True Then
                    '文件
                    Nit = GetImageIndex(FileFullName, True)

                    If Nit.IconIndex > 4 Then GoTo 999

                    If IsInTreeView(TreeView1, FileFullName) = True Then
                        GoTo 999
                    End If

                    Nod = TreeView1.Nodes.Add("", GetOnlyname(FileFullName), Nit.IconIndex, Nit.IconIndex)
                    KeyIndex = Nod.Index
                    Nod.ToolTipText = FileFullName

                    Get_Depend(Nod, FileFullName)
                End If
999:
            Next
        End If

        UnReg()
        TreeView1.EndUpdate()
        ToolStripStatusLabel1.Text = "扫描文件/文件夹完成"
        WaitEnd()

    End Sub

    '树菜单--替换
    Private Sub TSMenu_Replace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMenu_Replace.Click
        Dim NewFileName As String
        Dim Node As TreeNode

        Node = TreeView1.SelectedNode
        If Node Is Nothing Then Exit Sub

        With OpenFileDialog1
            .Title = "添加SLDASM\SLDPRT文件"
            .Filter = "SW模型(*.SLDASM;*.SLDPRT)|*.SLDASM;*.SLDPRT|SW装配(*.SLDASM)|*.SLDASM;|SW零件(*.SLDPRT)|*.SLDPRT"
            .InitialDirectory = GetParentFolder(TreeView1.SelectedNode.ToolTipText)
            Dim result As DialogResult = .ShowDialog()
            If result = DialogResult.OK Then
                NewFileName = Trim(.FileName)
            Else
                Exit Sub
            End If
        End With

        If NewFileName = "" Or NewFileName = Node.Text Then Exit Sub

        WaitStart()
        ListView3.BeginUpdate()

        ReplaceFile(TreeView1, Node, ListView3, NewFileName)

        ListView3.EndUpdate()
        WaitEnd()

    End Sub

    'Private Sub TreeView1_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick
    '    On Error Resume Next
    '    SLDDRWFile.Sldprt_File_Name = e.Node.ToolTipText
    '    ToolStripStatusLabel1.Text = SLDDRWFile.Sldprt_File_Name
    '    If isHaveDrawing(SLDDRWFile.Slddrw_File_Name) = "" Then
    '        ToolStripStatusLabel2.Text = ""
    '    Else
    '        ToolStripStatusLabel2.Text = "存在对应的工程图文件"
    '    End If

    '    If IsFileExsts(SLDDRWFile.Sldprt_File_Name) = True Then
    '        AxEModelViewControl1.OpenDoc(SLDDRWFile.Sldprt_File_Name, False, False, True, "")
    '    End If
    'End Sub


    '树菜单--折叠树
    Private Sub TSMenu_CollapseAll_Nodes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMenu_CollapseAll_Nodes.Click
        TreeView1.CollapseAll()
    End Sub

    '树菜单--展开树
    Private Sub TSMenu_ExpandAll_Nodes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMenu_ExpandAll_Nodes.Click
        TreeView1.ExpandAll()
    End Sub

    '文件列表菜单--打开关联文件
    Private Sub TSMenu_Open_Depend_File_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMenu_Open_Depend_File.Click
        Dim FileFullName As String = Nothing
        Dim LVI As ListViewItem

        If DoModel = DRW Then
            If ListView1.Items.Count > 0 Then
                For Each LVI In ListView1.SelectedItems
                    FileFullName = UCase(LVI.SubItems(1).Text)
                Next

                FileFullName = Strings.Replace(FileFullName, DRW, PRT)
                If IsFileExsts(FileFullName) = True Then
                    System.Diagnostics.Process.Start(FileFullName)
                    Exit Sub
                Else
                    FileFullName = Strings.Replace(FileFullName, PRT, ASM)
                    If IsFileExsts(FileFullName) = True Then
                        System.Diagnostics.Process.Start(FileFullName)
                        Exit Sub
                    End If

                End If
                MsgBox("未找到关联文件。", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "打开文件")
                ToolStripStatusLabel2.Text = "未找到关联的模型文件"
            End If
        ElseIf DoModel = PRT Then
            If TreeView1.Nodes.Count > 0 Then
                FileFullName = UCase(TreeView1.SelectedNode.ToolTipText)
                Select Case GetFileExtension(FileFullName)
                    Case ASM
                        FileFullName = Replace(FileFullName, ASM, DRW)
                    Case PRT
                        FileFullName = Replace(FileFullName, PRT, DRW)
                End Select

                If IsFileExsts(FileFullName) Then
                    System.Diagnostics.Process.Start(FileFullName)
                    Exit Sub
                Else
                    If MsgBox("未找到关联文件，是否生成工程图文件？", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "打开文件") = MsgBoxResult.Yes Then
                        FileFullName = UCase(TreeView1.SelectedNode.ToolTipText)
                        If CreateNewDrawing(FileFullName) = True Then

                            Select Case GetFileExtension(FileFullName)
                                Case ASM
                                    FileFullName = Replace(FileFullName, ASM, DRW)
                                Case PRT
                                    FileFullName = Replace(FileFullName, PRT, DRW)
                            End Select

                            If MsgBox("工程图：" & FileFullName & "  已生成,打开文件?", MsgBoxStyle.Information + MsgBoxStyle.YesNo, "打开文件") = MsgBoxResult.Yes Then
                                System.Diagnostics.Process.Start(FileFullName)
                            End If
                        Else
                            MsgBox("工程图未已生成，检查  设置->工程图模板。", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "打开文件")
                        End If
                    End If
                    ToolStripStatusLabel2.Text = "未找到关联文件"
                End If

            End If
        End If

    End Sub

    '属性列表菜单--双击属性列表修改
    Private Sub ListView2_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView2.MouseDoubleClick
        TSMenu_Modify_Click(sender, e)
    End Sub

    '显示属性列表菜单
    Private Sub ListView2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView2.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Right Then
            TSMenu_Batch_Edit.Enabled = True
            TSMenu_Modify.Enabled = True
            TSMenu_DeleteCustomProperty.Enabled = True
        End If
    End Sub

    '属性列表菜单--批量修改
    Private Sub TSMenu_Batch_Edit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMenu_Batch_Edit.Click
        Dim AttribName As String
        Dim OldAttribValue As String
        Dim NewAttribValue As String
        Dim LVI As ListViewItem


        If ListView2.SelectedItems.Count > 0 Then
            'For Each SelectList In ListView1.SelectedItems
            '    ListView1.Items.Remove(SelectList)
            'Next
            AttribName = ListView2.SelectedItems(0).Text
            OldAttribValue = ListView2.SelectedItems(0).SubItems(1).Text

            If IsTopMost = True Then
                Me.TopMost = False
            End If

            NewAttribValue = InputBox(AttribName & "  批量修改为:", "修改", OldAttribValue)

            If IsTopMost = True Then
                Me.TopMost = True
            End If

            If (Trim(NewAttribValue) = "") Then 'Or (NewAttribValue = OldAttribValue) Then
                Exit Sub
            End If

            LVI = ListView4.Items.Add("批量修改")
            LVI.SubItems.Add(AttribName)
            LVI.SubItems.Add(OldAttribValue)
            LVI.SubItems.Add(NewAttribValue)
            Set_LV_Color(ListView4, Color.LightCoral, Color.White)

        End If

    End Sub

    '属性列表菜单--修改
    Private Sub TSMenu_Modify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMenu_Modify.Click
        Dim AttribIndex As Short
        Dim AttribName As String
        Dim OldAttribValue As String
        Dim NewAttribValue As String
        'Dim SelectList As ListViewItem
        If ListView2.SelectedItems.Count > 0 Then
            'For Each SelectList In ListView1.SelectedItems
            '    ListView1.Items.Remove(SelectList)
            'Next
            AttribIndex = ListView2.SelectedItems(0).Index
            AttribName = ListView2.SelectedItems(0).Text
            OldAttribValue = ListView2.SelectedItems(0).SubItems(1).Text

            If IsTopMost = True Then
                Me.TopMost = False
            End If

            NewAttribValue = InputBox(OldAttribValue & "修改为:", "修改", OldAttribValue)

            If IsTopMost = True Then
                Me.TopMost = True
            End If

            If (Trim(NewAttribValue) = OldAttribValue) Or Trim(NewAttribValue) = "" Then
                Exit Sub
            End If

            If A_SLDDRWFile.CustomProperty(AttribIndex).LinkedTo <> "" Then
                MsgBox("自定义属性： " & AttribName & " 存在文字表达式，未作修改！", MsgBoxStyle.Information, "修改")
                Exit Sub
            End If

            ListView2.SelectedItems(0).SubItems(1).Text = NewAttribValue
            SetCustomProperty(A_SLDDRWFile.Slddrw_File_Name, AttribName, NewAttribValue, True)

            If AttribName = Part_Number Then
                ListView1.SelectedItems(0).SubItems(2).Text = NewAttribValue
            End If

        End If

    End Sub


    '树菜单--手动修复关联
    Private Sub TSMenu_Repair_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMenu_Repair.Click

        Dim New_File As SLDDRW_File
        Dim Old_File As SLDDRW_File

        Dim NodeIconText As NodeIconText

        Dim swApp As SldWorks
        swApp = CreateObject("SldWorks.Application")

        If ListView1.Items.Count > 0 And DoModel = DRW Then
            Old_File.Slddrw_File_Name = UCase(ListView1.SelectedItems(0).Text)
            Old_File.Sldprt_File_Name = Replace(Old_File.Slddrw_File_Name, DRW, PRT)
            If IsFileExsts(Old_File.Sldprt_File_Name) = False Then
                Old_File.Sldprt_File_Name = Replace(Old_File.Slddrw_File_Name, DRW, ASM)
            End If

            With OpenFileDialog1
                .Title = "添加SLDASM\SLDPRT文件"
                .Filter = "SW模型(*.SLDASM;*.SLDPRT)|*.SLDASM;*.SLDPRT|SW装配(*.SLDASM)|*.SLDASM;|SW零件(*.SLDPRT)|*.SLDPRT"
                .Multiselect = False
                Dim result As DialogResult = .ShowDialog()
                If result = DialogResult.OK Then
                    New_File.Sldprt_File_Name = Trim(.FileName)
                    New_File.Slddrw_File_Name = Replace(New_File.Sldprt_File_Name, GetFileExtension(New_File.Sldprt_File_Name), DRW)
                Else
                    Exit Sub
                End If
            End With

            ReFileName(Old_File.Slddrw_File_Name, New_File.Slddrw_File_Name)
            swApp.ReplaceReferencedDocument(New_File.Slddrw_File_Name, Old_File.Sldprt_File_Name, New_File.Sldprt_File_Name)    '更改工程图的参考关系
        ElseIf TreeView1.Nodes.Count > 0 And DoModel = PRT Then
            Dim Node As TreeNode
            Dim Parent_File As SLDDRW_File

            Node = TreeView1.SelectedNode
            If Node.SelectedImageIndex <> 3 Then
                ToolStripStatusLabel2.Text = "关联正常"
                Exit Sub
            End If

            Old_File.Sldprt_File_Name = UCase(Node.ToolTipText)            '选择的文件
            Parent_File.Sldprt_File_Name = UCase(Node.Parent.ToolTipText)       '父文件

            With OpenFileDialog1
                .Title = "添加SLDASM\SLDPRT文件"
                .Filter = "SW模型(*.SLDASM;*.SLDPRT)|*.SLDASM;*.SLDPRT|SW装配(*.SLDASM)|*.SLDASM;|SW零件(*.SLDPRT)|*.SLDPRT"
                .Multiselect = False
                Dim result As DialogResult = .ShowDialog()
                If result = DialogResult.OK Then
                    New_File.Sldprt_File_Name = Trim(.FileName)
                Else
                    Exit Sub
                End If
            End With
            swApp.ReplaceReferencedDocument(Parent_File.Sldprt_File_Name, Old_File.Sldprt_File_Name, New_File.Sldprt_File_Name)    '更改工程图的参考关系
            Node.Text = GetOnlyname(New_File.Sldprt_File_Name)
            Node.ToolTipText = New_File.Sldprt_File_Name


            NodeIconText = GetImageIndex(New_File.Sldprt_File_Name, False)
            Node.ImageIndex = NodeIconText.IconIndex
            Node.ForeColor = NodeIconText.TextForeColor
            Node.SelectedImageIndex = Node.ImageIndex
        End If

    End Sub

    '工具栏--自动修复父子关联
    Private Sub ToolStripButton_Rrepair_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton_Rrepair.Click
        TSMenu_File_AutoRepair_Click(sender, e)
    End Sub

    '树菜单--设置预览零件
    Private Sub TSMenu_File_Review_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMenu_File_Review.Click
        If Review = CheckState.Checked Then
            Review = CheckState.Unchecked
        ElseIf Review = CheckState.Unchecked Then
            Review = CheckState.Checked
        End If
        TSMenu_File_Review.Checked = Review
    End Sub

    '树菜单--自动修复父子关联
    Private Sub TSMenu_File_AutoRepair_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMenu_File_AutoRepair.Click
        Dim Node As TreeNode

        WaitStart()
        ListView3.BeginUpdate()

        For Each Node In TreeView1.Nodes   '遍历第一层
            Auto_Repair_Link(TreeView1, ListView3, Node)
        Next

        ListView3.EndUpdate()
        WaitEnd()

        ToolStripStatusLabel2.Text = "自动修复父子关联完成"
    End Sub

    '文件列表菜单--打开文件所在文件夹
    Private Sub TSMenu_Open_Folder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMenu_Open_Folder.Click
        Dim LVI As ListViewItem
        Dim FileFullName As String = Nothing
        Dim ParentFolder As String = Nothing

        If DoModel = DRW Then
            If ListView1.Items.Count > 0 Then
                For Each LVI In ListView1.SelectedItems
                    FileFullName = LVI.SubItems(1).Text
                Next
            Else
                Exit Sub
            End If
        ElseIf DoModel = PRT Then
            If TreeView1.Nodes.Count > 0 Then
                FileFullName = TreeView1.SelectedNode.ToolTipText
            Else
                Exit Sub
            End If
        End If

        ParentFolder = GetParentFolder(FileFullName)
        If IsDirectoryExists(ParentFolder) = True Then
            System.Diagnostics.Process.Start(ParentFolder)
        Else
            MsgBox(ParentFolder & "打开错误。", MsgBoxStyle.OkOnly & MsgBoxStyle.Critical)
        End If
    End Sub

    '文件列表交换顺序
    Private Sub ListView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ListView1.KeyDown
        If ListView1.SelectedIndices.Count > 0 Then 'And e.Modifiers = Keys.Control Then
            Select Case e.KeyCode & e.Modifiers
                Case Keys.Up & Keys.Control
                    ListView_MoveUp(ListView1)
                Case Keys.Down & Keys.Control
                    ListView_MoveDown(ListView1)
                Case Keys.Delete & 0
                    Menu_ReMove_List_Click(sender, e)
            End Select

            'ReBuiltNumber(ListView1)
            'Set_LV_Color(ListView1, Color.LightBlue, Color.White)
        End If

    End Sub

    '菜单--自动按顺序修改文件图号
    Private Sub Menu_Follow_NO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_Follow_NO.Click
        Dim LVI As ListViewItem
        Dim i As Integer
        Dim prefix As String
        Dim Length As Integer
        Dim DwgNo As String

        If ListView1.Items.Count > 0 Then
            prefix = InputBox("图号前缀，例：AA-01，输入AA-", "前缀")
            If prefix = "" Then Exit Sub

            i = InputBox("图号开始数值，例：AA-01,输入1", "开始数值", "1")
            'If i = "" Then Exit Sub

            Length = InputBox("图号长度，例：AA-01,输入2", "长度", "2")
            'If Length = "" Then Exit Sub

            Me.Cursor = Cursors.WaitCursor
            ToolStripProgressBar1.Minimum = 1
            ToolStripProgressBar1.Maximum = ListView1.Items.Count
            For Each LVI In ListView1.Items
                'LVI.Selected = True

                '添加任务代码
                '--------------------------------
                DwgNo = prefix & Microsoft.VisualBasic.Strings.Right("0000" & i, Length)
                SetCustomProperty(LVI.SubItems(1).Text, Part_Number, DwgNo, True)
                LVI.SubItems(2).Text = DwgNo

                '----------------------------------
                i = i + 1

                ToolStripStatusLabel2.Text = (LVI.Index + 1) * 100 \ ToolStripProgressBar1.Maximum & "%"
                ToolStripProgressBar1.Value = LVI.Index + 1
            Next
        End If

        ToolStripStatusLabel1.Text = "按列表顺序修改图号完成"
        Me.Cursor = Cursors.Default
        MsgBox("按列表顺序修改图号完成", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "顺序修改图号")

    End Sub

    '文件列表菜单--复制自定义属性
    Private Sub TSMenu_CopyAttrib_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMenu_CopyAttrib.Click
        If ListView1.Items.Count = 0 Or ListView1.SelectedItems.Count = 0 Then
            Exit Sub
        End If

        If SelecdItem Is Nothing = False Then
            SelecdItem.BackColor = SelecdColor
        End If
        Copyed_SLDDRWFile = Nothing

        Copyed_SLDDRWFile.Slddrw_File_Name = ListView1.SelectedItems(0).SubItems(1).Text

        If IsFileExsts(Copyed_SLDDRWFile.Slddrw_File_Name) = False Then Exit Sub

        Copyed_SLDDRWFile = GetCustomPropertys(Copyed_SLDDRWFile, True)

    End Sub

    '文件列表菜单--粘贴自定义属性
    Private Sub TSMenu_PasteAttrib_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMenu_PasteAttrib.Click
        If ListView1.Items.Count = 0 Or ListView1.SelectedItems.Count = 0 Then
            Exit Sub
        End If

        Copyed_SLDDRWFile.Slddrw_File_Name = ListView1.SelectedItems(0).SubItems(1).Text
        SetCustomPropertys(Copyed_SLDDRWFile, True)

        A_SLDDRWFile = Copyed_SLDDRWFile

        ReFreshCustomProperty(A_SLDDRWFile, ListView2)

        ListView1.SelectedItems(0).SubItems(2).Text = GetCustomProperty(A_SLDDRWFile.Slddrw_File_Name, Part_Number, True)

        ToolStripStatusLabel1.Text = ListView1.SelectedItems(0).Index + 1 & "#" & A_SLDDRWFile.Slddrw_File_Name

        A_SLDDRWFile.Part_Name = GetOnlyname(A_SLDDRWFile.Slddrw_File_Name)
        A_SLDDRWFile.Parent_Folder = My.Computer.FileSystem.GetFileInfo(A_SLDDRWFile.Slddrw_File_Name).DirectoryName

        A_SLDDRWFile.Sldprt_File_Name = A_SLDDRWFile.Parent_Folder & "\" & A_SLDDRWFile.Part_Name & PRT
        If IsFileExsts(A_SLDDRWFile.Sldprt_File_Name) = True Then
            ToolStripStatusLabel2.Text = "存在关联的零件文件"
            Exit Sub
        End If

        A_SLDDRWFile.Sldprt_File_Name = A_SLDDRWFile.Parent_Folder & "\" & A_SLDDRWFile.Part_Name & ASM
        If IsFileExsts(A_SLDDRWFile.Sldprt_File_Name) = True Then
            ToolStripStatusLabel2.Text = "存在关联的装配文件"
            Exit Sub
        End If

        ToolStripStatusLabel2.Text = "未找到关联的模型文件"
    End Sub

    '菜单--另存为pdf
    Private Sub Menu_Save_As_Pdf_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_Save_As_Pdf.Click
        Dim LVI As ListViewItem

        If ListView1.CheckedItems.Count > 0 Then
            With ToolStripProgressBar1
                .Minimum = 0
                .Maximum = ListView1.CheckedItems.Count
                .Value = 0
            End With

            If CAD_To = 1 Then
                With FolderBrowserDialog1
                    .Description = "另存为PDF到文件夹"
                    .ShowNewFolderButton = True
                    .RootFolder = System.Environment.SpecialFolder.Desktop
                    Dim result As DialogResult = .ShowDialog()
                    If result = DialogResult.OK Then
                        A_SLDDRWFile.Parent_Folder = .SelectedPath
                    Else
                        ToolStripStatusLabel1.Text = "另存为PDF完成"
                        Me.Cursor = Cursors.Default
                        Exit Sub
                    End If
                End With
            Else
                A_SLDDRWFile.Parent_Folder = ""
            End If

            Me.Cursor = Cursors.WaitCursor
            For Each LVI In ListView1.CheckedItems
                'FList.Selected = True

                '添加任务代码
                '--------------------------------
                Application.DoEvents()
                A_SLDDRWFile.Slddrw_File_Name = LVI.SubItems(1).Text

                If IsFileExsts(A_SLDDRWFile.Slddrw_File_Name) = True Then

                    ToolStripStatusLabel1.Text = "进程文件：" & A_SLDDRWFile.Slddrw_File_Name

                    'GetCustomPropertys()

                    Save_as_Pdf(A_SLDDRWFile.Parent_Folder, A_SLDDRWFile)
                End If
                '----------------------------------

                ToolStripProgressBar1.Value = ToolStripProgressBar1.Value + 1
                ToolStripStatusLabel2.Text = Val(ToolStripProgressBar1.Value.ToString) * 100 \ ToolStripProgressBar1.Maximum & "%"

            Next
        End If
        ToolStripStatusLabel1.Text = "另存为PDF完成"
        Me.Cursor = Cursors.Default
        MsgBox("另存为PDF完成", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "PDF")
    End Sub

    ''edrawing视图
    'Private Sub ToolStripButton_Front_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    AxEModelViewControl1.ViewOrientation = 0
    'End Sub

    'Private Sub ToolStripButton_Back_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    AxEModelViewControl1.ViewOrientation = 1
    'End Sub

    'Private Sub ToolStripButton_Left_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    AxEModelViewControl1.ViewOrientation = 4
    'End Sub

    'Private Sub ToolStripButton_Right_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    AxEModelViewControl1.ViewOrientation = 5
    'End Sub

    'Private Sub ToolStripButton_Top_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    AxEModelViewControl1.ViewOrientation = 2
    'End Sub

    'Private Sub ToolStripButton_Bottom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    AxEModelViewControl1.ViewOrientation = 3
    'End Sub

    'Private Sub ToolStripButton_Isometric_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    AxEModelViewControl1.ViewOrientation = 6
    'End Sub


    '菜单--导入装配顺序
    Private Sub Menu_Input_Asm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_Input_Asm.Click
        On Error Resume Next
        Dim LVI As ListViewItem
        Dim SubName As String
        Dim SubFullName As String

        Dim vDepend As Object

        Dim swApp As SldWorks
        swApp = CreateObject("SldWorks.Application")

        With OpenFileDialog1
            .Multiselect = True
            .Title = "导入SLDASM文件"
            .Filter = "SW装配(*.SLDASM)|*.SLDASM"
            If .ShowDialog() = DialogResult.OK Then
                FilesList = .FileNames
            Else
                Exit Sub
            End If
        End With

        'ListView1.Items.Clear()
        ListView1.BeginUpdate()

        For Each A_SLDDRWFile.Sldprt_File_Name In FilesList

            A_SLDDRWFile.Slddrw_File_Name = ChangeFileExe(A_SLDDRWFile.Sldprt_File_Name, DRW)
            If IsFileExsts(A_SLDDRWFile.Slddrw_File_Name) = True Then
                If InListView(ListView1, A_SLDDRWFile.Slddrw_File_Name) = -1 Then

                    LVI = ListView1.Items.Add(ListView1.Items.Count + 1)
                    LVI.SubItems.Add(A_SLDDRWFile.Slddrw_File_Name)
                    LVI.SubItems.Add(GetCustomProperty(A_SLDDRWFile.Slddrw_File_Name, Part_Number, True))

                End If
            End If

            vDepend = Nothing
            vDepend = swApp.GetDocumentDependencies2(A_SLDDRWFile.Sldprt_File_Name, True, True, False)

            If vDepend Is Nothing = False Then
                For Index = 0 To (UBound(vDepend) - 1) / 2
                    SubName = vDepend(2 * Index)   '简名
                    SubFullName = vDepend(2 * Index + 1)  '全名
                    A_SLDDRWFile.Slddrw_File_Name = ChangeFileExe(SubFullName, DRW)
                    If IsFileExsts(A_SLDDRWFile.Slddrw_File_Name) = True And InListView(ListView1, A_SLDDRWFile.Slddrw_File_Name) = -1 Then

                        LVI = ListView1.Items.Add(ListView1.Items.Count + 1)
                        LVI.SubItems.Add(A_SLDDRWFile.Slddrw_File_Name)
                        LVI.SubItems.Add(GetCustomProperty(A_SLDDRWFile.Slddrw_File_Name, Part_Number, True))

                    End If
                Next
            End If
        Next

        UnReg()

        ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)
        ListView1.Columns(0).Width = 40
        If ListView1.Columns(1).Width < (ListView1.Width - 100) Then
            ListView1.Columns(1).Width = ListView1.Width - 100
        End If

        ReBuiltNumber(ListView1)
        Set_LV_Color(ListView1, Color.LightBlue, Color.White)
        ListView1.EndUpdate()

    End Sub

    '属性列表菜单--删除自定义属性
    Private Sub TSMenu_DeleteCustomProperty_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMenu_DeleteCustomProperty.Click
        Dim AttribName As String
        'Dim SelectList As ListViewItem
        If ListView2.SelectedItems.Count > 0 Then
            'For Each SelectList In ListView1.SelectedItems
            '    ListView1.Items.Remove(SelectList)
            'Next
            AttribName = ListView2.SelectedItems(0).Text

            If MsgBox("删除自定义属性 " & AttribName & " ?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "自定义属性") = MsgBoxResult.Yes Then
                DeleteCustomProperty(A_SLDDRWFile.Slddrw_File_Name, AttribName, True)
                ListView2.SelectedItems(0).Remove()
                Set_LV_Color(ListView2, Color.BlanchedAlmond, Color.White)
            End If

        End If
    End Sub

    '菜单--生成bom
    Private Sub Menu_BOM_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_BOM.Click
        'On Error Resume Next
        Dim Csv_File As String = Nothing
        Dim LVI As ListViewItem
        'Dim i As Integer


        If ListView1.CheckedItems.Count > 0 Then

            Me.Cursor = Cursors.WaitCursor

            With ToolStripProgressBar1
                .Minimum = 0
                .Maximum = ListView1.CheckedItems.Count
                .Value = 0
            End With

            Randomize()
            Csv_File = My.Computer.FileSystem.SpecialDirectories.Temp & "\swtool_bom_temp_" & Trim(Str(Int(Rnd() * 10000) + 1)) & ".csv"

            If IsFileExsts(Csv_File) = True Then
                System.IO.File.Delete(Csv_File)
            End If

            Dim Ini As New Ini
            Dim ArrayRow As String
            Dim Rows() As String


            ArrayRow = Ini.INIRead(Ini_File, "SwTool", " Csv_Attribs")
            WirteLineString(Csv_File, "文件名（零件名称）," & ArrayRow)

            Rows = Split(ArrayRow, ",")

            For Each LVI In ListView1.CheckedItems

                '添加任务代码
                '--------------------------------
                Application.DoEvents()
                A_SLDDRWFile.Slddrw_File_Name = LVI.SubItems(1).Text

                If IsFileExsts(A_SLDDRWFile.Slddrw_File_Name) = True Then
                    ToolStripStatusLabel1.Text = "进程文件：" & A_SLDDRWFile.Slddrw_File_Name
                    '读取文件自定义属性
                    A_SLDDRWFile = GetCustomPropertys(A_SLDDRWFile, True)

                    'ListView2.BeginUpdate()
                    'ListView2.Items.Clear()
                    'For i = 0 To A_SLDDRWFile.CustomPropertyCount
                    '    Dim LV As New ListViewItem(A_SLDDRWFile.CustomProperty(i).Name)
                    '    LV.SubItems.Add(A_SLDDRWFile.CustomProperty(i).Evaluated)
                    '    LV.SubItems.Add("")
                    '    ListView2.Items.Add(LV)
                    'Next
                    'Set_LV_Color(ListView2, Color.BlanchedAlmond, Color.White)
                    'ListView2.EndUpdate()

                    '写入csv文件
                    Write_Csv(Csv_File, A_SLDDRWFile, Rows)
                End If

                '----------------------------------

                ToolStripProgressBar1.Value = ToolStripProgressBar1.Value + 1
                ToolStripStatusLabel2.Text = Val(ToolStripProgressBar1.Value.ToString) * 100 \ ToolStripProgressBar1.Maximum & "%"

            Next
        End If
        ListView4.Items.Clear()
        ToolStripStatusLabel1.Text = "生成BOM完成"
        Me.Cursor = Cursors.Default
        MsgBox("生成BOM完成", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "BOM")

        Try
            Process.Start(Csv_File)
        Catch ex As Exception

        End Try


    End Sub

    '属性列表菜单--批量删除
    Private Sub TSMenu_Batch_Del_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMenu_Batch_Del.Click
        Dim AttribName As String
        Dim LVI As ListViewItem

        If ListView2.SelectedItems.Count > 0 Then
            AttribName = ListView2.SelectedItems(0).Text

            If MsgBox("批量删除 " & AttribName & " ?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "自定义属性") = MsgBoxResult.Yes Then
                LVI = ListView4.Items.Add("批量删除")
                LVI.SubItems.Add(AttribName)
                Set_LV_Color(ListView4, Color.LightCoral, Color.White)
            End If
        Else
            Exit Sub
        End If
    End Sub

    '属性列表菜单--新增属性
    Private Sub TSMenu_Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMenu_Add.Click
        Dim AttribName As String
        Dim AttribValue As String

        If IsTopMost = True Then
            Me.TopMost = False
        End If

        AttribName = InputBox("新增自定义属性名称：", "新增")
        If (Trim(AttribName) = "") Then
            GoTo 999
        End If

        AttribValue = InputBox("新增自定义属性 " & AttribName & "  值：", "新增")

        If Trim(AttribValue) = "" Then
            GoTo 999
        End If

        AddCustomProperty(A_SLDDRWFile.Slddrw_File_Name, AttribName, AttribValue, True)

        A_SLDDRWFile = ReFreshCustomProperty(A_SLDDRWFile, ListView2)

        'Dim LV As New ListViewItem(AttribName)
        'LV.SubItems.Add(AttribValue)
        'LV.SubItems.Add("")
        'ListView2.Items.Add(LV)
        'Set_LV_Color(ListView2, Color.BlanchedAlmond, Color.White)
999:
        If IsTopMost = True Then
            Me.TopMost = True
        End If

    End Sub

    '属性列表菜单--批量新增
    Private Sub TSMenu_Batch_Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMenu_Batch_Add.Click
        Dim LVI As ListViewItem
        Dim AttribName As String = ""
        Dim AttribValue As String = ""

        If IsTopMost = True Then
            Me.TopMost = False
        End If

        'AttribName = ListView2.SelectedItems(0).Text

        AttribName = InputBox("批量新增自定义属性名称：", "批量新增", AttribName)
        If Trim(AttribName) = "" Then
            GoTo 999
        End If

        'AttribValue = ListView2.SelectedItems(0).SubItems(1).Text
        AttribValue = InputBox("批量新增自定义属性 " & AttribName & "  值：", "批量新增", AttribValue)

        If Trim(AttribValue) = "" Then
            GoTo 999
        End If

        LVI = ListView4.Items.Add("批量新增")
        LVI.SubItems.Add(AttribName)
        LVI.SubItems.Add("-")
        LVI.SubItems.Add(AttribValue)
        Set_LV_Color(ListView4, Color.LightCoral, Color.White)

999:
        If IsTopMost = True Then
            Me.TopMost = True
        End If
    End Sub

    '文件列表序号项宽度
    Private Sub ListView1_ColumnWidthChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnWidthChangedEventArgs) Handles ListView1.ColumnWidthChanged
        On Error Resume Next
        If ListView1.Columns(0).Width <> 50 Then
            ListView1.Columns(0).Width = 50
        End If
    End Sub

    '文件列表菜单--直接使用默认打印机打印文件
    Private Sub TSMenu_Print_File_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMenu_Print_File.Click
        Dim PrinterName As String = Nothing
        Dim LVI As ListViewItem


        If (PrinterSettings.InstalledPrinters.Count <= 0) Then
            MessageBox.Show("没找到打印机 ")
        End If

        Dim ps As New PrinterSettings
        Dim printName As String
        For Each printName In PrinterSettings.InstalledPrinters
            ps.PrinterName = printName
            If ps.IsDefaultPrinter = True Then
                PrinterName = ps.PrinterName
                Exit For
            End If
        Next

        Me.Cursor = Cursors.Default
        If ListView1.Items.Count > 0 Then
            For Each LVI In ListView1.SelectedItems
                'FList.Selected = True
                '添加任务代码
                '--------------------------------
                Application.DoEvents()
                A_SLDDRWFile.Slddrw_File_Name = LVI.SubItems(1).Text

                If IsFileExsts(A_SLDDRWFile.Slddrw_File_Name) = True Then
                    ToolStripStatusLabel1.Text = "进程文件：" & A_SLDDRWFile.Slddrw_File_Name

                    PrintDrawing(A_SLDDRWFile.Slddrw_File_Name, PrinterName, 1)
                End If

                '----------------------------------

            Next
        End If
        ToolStripStatusLabel1.Text = "打印完成"
        PrintDocument1.Dispose()
        Me.Cursor = Cursors.Default
        MsgBox("打印完成", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "打印")
    End Sub

    '文件列表菜单--另存为cad
    Private Sub TSMenu_Saveas_CAD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMenu_Saveas_CAD.Click
        Dim LVI As ListViewItem
        Me.Cursor = Cursors.WaitCursor
        If ListView1.Items.Count > 0 Then

            A_SLDDRWFile.Parent_Folder = ""

            For Each LVI In ListView1.SelectedItems
                '添加任务代码
                '--------------------------------
                Application.DoEvents()
                A_SLDDRWFile.Slddrw_File_Name = LVI.SubItems(1).Text

                If IsFileExsts(A_SLDDRWFile.Slddrw_File_Name) = True Then

                    ToolStripStatusLabel1.Text = "进程文件：" & A_SLDDRWFile.Slddrw_File_Name

                    'GetCustomPropertys()

                    Save_as_Dwg(A_SLDDRWFile.Parent_Folder, A_SLDDRWFile)
                End If

                '----------------------------------
            Next
        End If
        ToolStripStatusLabel1.Text = "另存为CAD完成"
        Me.Cursor = Cursors.Default
        MsgBox("另存为CAD完成", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "CAD")
    End Sub

    '冒泡法排序
    Private Sub PaiXu(ByVal ListView As ListView, ByVal PartNo As String)
        Dim LVI As ListViewItem
        Dim i As Short
        Dim j As Short
        Dim bound As Short

        Dim PaiXu() As PaiXu
        Dim PaiXu_Temp As PaiXu

        bound = ListView.Items.Count - 1

        ReDim PaiXu(bound)

        i = 0
        For i = 0 To bound
            PaiXu(i).FileFullName = ListView.Items(i).SubItems(1).Text
            PaiXu(i).PartNumber = GetCustomProperty(PaiXu(i).FileFullName, PartNo, True)

        Next

        For i = 0 To bound - 1
            For j = i + 1 To bound
                If PaiXu(i).PartNumber > PaiXu(j).PartNumber Then
                    PaiXu_Temp = PaiXu(i)
                    PaiXu(i) = PaiXu(j)
                    PaiXu(j) = PaiXu_Temp
                End If
            Next
        Next

        ListView.Items.Clear()
        ListView.BeginUpdate()
        For i = 0 To bound
            LVI = ListView.Items.Add(i + 1)
            LVI.SubItems.Add(PaiXu(i).FileFullName)
        Next
        Set_LV_Color(ListView, Color.LightBlue, Color.White)
        ListView.EndUpdate()


    End Sub

    '属性列表菜单--批量替换
    Private Sub TSMenu_Batch_Change_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMenu_Batch_Change.Click
        Dim AttribName As String
        Dim OldAttribValue As String
        Dim NewAttribValue As String
        Dim LVI As ListViewItem


        If ListView2.SelectedItems.Count > 0 Then

            AttribName = ListView2.SelectedItems(0).Text
            OldAttribValue = ListView2.SelectedItems(0).SubItems(1).Text

            If IsTopMost = True Then
                Me.TopMost = False
            End If

            OldAttribValue = InputBox("批量替换的旧值:", "批量替换", OldAttribValue)
            NewAttribValue = InputBox(OldAttribValue & "  批量替换为:", "批量替换", OldAttribValue)

            If IsTopMost = True Then
                Me.TopMost = True
            End If

            If (Trim(NewAttribValue) = "") Or (NewAttribValue = OldAttribValue) Or (Trim(OldAttribValue) = "") Then
                Exit Sub
            End If

            LVI = ListView4.Items.Add("批量替换")
            LVI.SubItems.Add(AttribName)
            LVI.SubItems.Add(OldAttribValue)
            LVI.SubItems.Add(NewAttribValue)
            Set_LV_Color(ListView4, Color.LightCoral, Color.White)
        End If

    End Sub

    '任务列表菜单--移除
    Private Sub TSMenu_RemoveBatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMenu_RemoveBatch.Click
        'Dim SelectList As ListViewItem
        If ListView4.SelectedItems.Count > 0 Then
            'For Each SelectList In ListView1.SelectedItems
            '    ListView1.Items.Remove(SelectList)
            'Next
            ListView4.SelectedItems(0).Remove()
            Set_LV_Color(ListView4, Color.LightCoral, Color.White)
        End If
    End Sub

    '菜单--制作工程图
    Private Sub Menu_CreateNewDrawing_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_CreateNewDrawing.Click
        On Error Resume Next
        Dim SubName As String
        Dim SubFullName As String
        Dim vDepend As Object
        Dim swApp As SldWorks

        A_SLDDRWFile = Nothing
        With OpenFileDialog1
            .Multiselect = True
            .Title = "添加SLDASM\SLDPRT文件"
            .Filter = "SW模型(*.SLDASM;*.SLDPRT)|*.SLDASM;*.SLDPRT|SW装配(*.SLDASM)|*.SLDASM;|SW零件(*.SLDPRT)|*.SLDPRT"
            If .ShowDialog() = DialogResult.OK Then
                FilesList = .FileNames
            Else
                Exit Sub
            End If
        End With

        swApp = CreateObject("SldWorks.Application")

        Me.Cursor = Cursors.WaitCursor
        Application.DoEvents()
        WaitStart()

        For Each A_SLDDRWFile.Sldprt_File_Name In FilesList
            Select Case GetFileExtension(A_SLDDRWFile.Sldprt_File_Name)
                Case ASM

                    A_SLDDRWFile.Slddrw_File_Name = ChangeFileExe(A_SLDDRWFile.Sldprt_File_Name, DRW)
                    If IsFileExsts(A_SLDDRWFile.Slddrw_File_Name) = False Then
                        CreateNewDrawing(A_SLDDRWFile.Sldprt_File_Name)
                    End If

                    vDepend = Nothing
                    vDepend = swApp.GetDocumentDependencies2(A_SLDDRWFile.Sldprt_File_Name, True, True, False)

                    If vDepend Is Nothing = False Then
                        For Index = 0 To (UBound(vDepend) - 1) / 2
                            SubName = vDepend(2 * Index)   '简名
                            SubFullName = vDepend(2 * Index + 1)  '全名
                            A_SLDDRWFile.Slddrw_File_Name = ChangeFileExe(SubFullName, DRW)
                            If IsFileExsts(A_SLDDRWFile.Slddrw_File_Name) = False Then
                                CreateNewDrawing(SubFullName)
                            End If

                        Next
                    End If

                Case PRT
                    A_SLDDRWFile.Slddrw_File_Name = ChangeFileExe(A_SLDDRWFile.Sldprt_File_Name, DRW)
                    If IsFileExsts(A_SLDDRWFile.Slddrw_File_Name) = False Then
                        CreateNewDrawing(A_SLDDRWFile.Sldprt_File_Name)
                    End If
            End Select
        Next

        WaitEnd()
        ToolStripStatusLabel1.Text = "从零件/装配体制作工程图完成"
        Me.Cursor = Cursors.Default
        MsgBox("从零件/装配体制作工程图完成", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "制作工程图")

    End Sub

    '属性列表菜单--复制单项
    Private Sub TSMenu_CopyOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMenu_CopyOne.Click
        Dim FileFullName As String
        If ListView2.SelectedItems.Count > 0 Then
            FileFullName = ListView1.SelectedItems(0).SubItems(1).Text
            If IsFileExsts(FileFullName) = False Then
                Exit Sub
            End If
            Copyed_CustomProperty.Name = ListView2.SelectedItems(0).Text
            Copyed_CustomProperty.Value = GetCustomProperty(FileFullName, Copyed_CustomProperty.Name, True)
        End If
    End Sub

    '属性列表菜单--粘贴单项
    Private Sub TSMenu_PasteOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMenu_PasteOne.Click
        Dim FileFullName As String
        If ListView2.SelectedItems.Count > 0 Then
            FileFullName = ListView1.SelectedItems(0).SubItems(1).Text
            If IsFileExsts(FileFullName) = False Then
                Exit Sub
            End If

            For i = 0 To ListView2.Items.Count - 1
                If ListView2.Items(i).Text = Copyed_CustomProperty.Name Then   '属性名称存在修改
                    SetCustomProperty(FileFullName, Copyed_CustomProperty.Name, Copyed_CustomProperty.Value, True)
                    Exit Sub
                End If
            Next

            AddCustomProperty(FileFullName, Copyed_CustomProperty.Name, Copyed_CustomProperty.Value, True)
            Dim LV As New ListViewItem(Copyed_CustomProperty.Name)
            LV.SubItems.Add(Copyed_CustomProperty.Value)
            LV.SubItems.Add("")
            ListView2.Items.Add(LV)
            Set_LV_Color(ListView2, Color.BlanchedAlmond, Color.White)
        End If
    End Sub

    '菜单--替换图纸格式
    Private Sub Menu_SetupSheet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_SetupSheet.Click
        Dim Ini As New Ini
        Dim Templates_Slddrt As String

        Templates_Slddrt = Ini.INIRead(Ini_File, "SwTool", "Templates_Slddrt")
        If IsFileExsts(Templates_Slddrt) = False Then
            MsgBox("默认图纸格式文件不存在，请到 设置 中选择", MsgBoxStyle.Critical)
            Exit Sub
        End If

        Select Case MsgBox("确定使用默认文件　" & Templates_Slddrt & "　替换图纸格式?", MsgBoxStyle.Question + MsgBoxStyle.YesNoCancel, "替换格式")
            Case MsgBoxResult.Yes

            Case MsgBoxResult.No
                Dim swApp As SldWorks

                swApp = CreateObject("SldWorks.Application")

                With OpenFileDialog1
                    .Reset()
                    .Multiselect = False
                    .InitialDirectory = swApp.GetUserPreferenceStringValue(swFileLocationsSheetFormat)
                    .Title = "设置 Slddrt 图纸格式文件"
                    .Filter = "SW工程图模板(*.Slddrt)|*.slddrt"
                    Dim result As DialogResult = .ShowDialog()
                    If result = DialogResult.OK Then
                        Templates_Slddrt = .FileName
                    Else
                        Exit Sub
                    End If
                End With
            Case MsgBoxResult.Cancel
                Exit Sub
        End Select

        Me.Cursor = Cursors.WaitCursor

        If ListView1.CheckedItems.Count > 0 Then
            With ToolStripProgressBar1
                .Minimum = 0
                .Maximum = ListView1.CheckedItems.Count
                .Value = 0
            End With

            For Each LVI In ListView1.CheckedItems
                '添加任务代码
                '--------------------------------
                Application.DoEvents()
                A_SLDDRWFile.Slddrw_File_Name = LVI.SubItems(1).Text

                If IsFileExsts(A_SLDDRWFile.Slddrw_File_Name) = True Then
                    ToolStripStatusLabel1.Text = "进程文件：" & A_SLDDRWFile.Slddrw_File_Name

                    SetUpDrawingSheet(A_SLDDRWFile.Slddrw_File_Name, Templates_Slddrt)
                End If

                '----------------------------------
                ToolStripProgressBar1.Value = ToolStripProgressBar1.Value + 1
                ToolStripStatusLabel2.Text = Val(ToolStripProgressBar1.Value.ToString) * 100 \ ToolStripProgressBar1.Maximum & "%"

            Next

        End If


        If MsgBox("批量替换图纸格式完成，是否复制替换来自文件的自定义项？", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "图纸格式") = MsgBoxResult.Yes Then

            With OpenFileDialog1
                .Multiselect = False
                .Title = "Slddrw文件"
                .Filter = "SW工程图(*.Slddrw)|*.slddrw"
                Dim result As DialogResult = .ShowDialog()
                If result = DialogResult.OK Then
                    Copyed_SLDDRWFile.Slddrw_File_Name = .FileName
                Else
                    ToolStripStatusLabel1.Text = "更新图纸格式完成"
                    Me.Cursor = Cursors.Default
                    Exit Sub
                End If
            End With

            Copyed_SLDDRWFile = GetCustomPropertys(Copyed_SLDDRWFile, True)

            With ToolStripProgressBar1
                .Minimum = 0
                .Maximum = ListView1.CheckedItems.Count
                .Value = 0
            End With

            For Each LVI In ListView1.CheckedItems
                '添加任务代码
                '--------------------------------
                Application.DoEvents()
                A_SLDDRWFile.Slddrw_File_Name = LVI.SubItems(1).Text

                If IsFileExsts(A_SLDDRWFile.Slddrw_File_Name) = True Then
                    ToolStripStatusLabel1.Text = "进程文件：" & A_SLDDRWFile.Slddrw_File_Name

                    DeleteCustomPropertys(A_SLDDRWFile.Slddrw_File_Name, True)

                    Copyed_SLDDRWFile.Slddrw_File_Name = A_SLDDRWFile.Slddrw_File_Name

                    SetCustomPropertys(Copyed_SLDDRWFile, True)

                End If

                '----------------------------------
                ToolStripProgressBar1.Value = ToolStripProgressBar1.Value + 1
                ToolStripStatusLabel2.Text = Val(ToolStripProgressBar1.Value.ToString) * 100 \ ToolStripProgressBar1.Maximum & "%"

            Next
            MsgBox("批量复制替换来自文件的自定义项完成", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "图纸格式")
        End If

        ToolStripStatusLabel1.Text = "更新图纸格式完成"
        Me.Cursor = Cursors.Default

    End Sub

    '菜单--是否获取图号
    Private Sub Menu_GetNO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_GetNO.Click
        Select Case IsGetNo
            Case 0
                IsGetNo = 1
                Menu_GetNO.CheckState = CheckState.Checked
            Case 1
                IsGetNo = 0
                Menu_GetNO.CheckState = CheckState.Unchecked
        End Select

    End Sub


    '菜单--删除文件
    Private Sub Menu_KillFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_KillFile.Click

        If MsgBox("是否删除选择的文件到回收站？", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "删除文件") = MsgBoxResult.No Then
            Exit Sub
        End If

        Select Case DoModel
            Case DRW
                For Each LVI As ListViewItem In ListView1.CheckedItems
                    DelFile(LVI.SubItems(1).Text.ToString, FileIO.RecycleOption.SendToRecycleBin)
                    If IsFileExsts(LVI.SubItems(1).Text.ToString) = False Then
                        LVI.Remove()
                        MsgBox("删除文件完成", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "删除文件")
                    Else
                        MsgBox("文件未删除", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "删除文件")
                    End If
                Next

                ReBuiltNumber(ListView1)
                Set_LV_Color(ListView1, Color.LightBlue, Color.White)

            Case PRT
                If TreeView1.SelectedNode.Parent Is Nothing Then
                    DelFile(TreeView1.SelectedNode.ToolTipText.ToString, FileIO.RecycleOption.SendToRecycleBin)
                    If IsFileExsts(TreeView1.SelectedNode.ToolTipText.ToString) = False Then
                        TreeView1.SelectedNode.Remove()
                        MsgBox("删除文件完成", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "删除文件")
                    Else
                        MsgBox("文件未删除", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "删除文件")
                    End If
                Else
                    MsgBox("仅支持第一级文件", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "删除文件")
                End If
        End Select
    End Sub

    '工具栏--列表向上移动
    Private Sub ToolStripButton_MoveUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton_MoveUp.Click
        ListView_MoveUp(ListView1)
    End Sub

    '工具栏--列表向下移动
    Private Sub ToolStripButton_MoveDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton_MoveDown.Click
        ListView_MoveDown(ListView1)
    End Sub

    ' 將選取的項目往上移動一個位置
    Private Sub ListView_MoveUp(ByVal LV As ListView)
        LV.BeginUpdate()
        '檢查有沒有選取項目
        If LV.CheckedIndices.Count > 0 Then
            '用for迴圈由小到大去巡覽
            For i As Integer = 0 To LV.CheckedIndices.Count - 1
                Dim index As Integer = LV.CheckedIndices(i)
                '如果index為第一項就不需要上移
                If index > 0 Then
                    '如果index-1被選取就不進行上移
                    If LV.CheckedIndices.Contains(index - 1) Then
                        Continue For
                    End If
                    '進行換位置的動作
                    MoveListViewItem(LV, index, True)
                End If
            Next
        ElseIf LV.SelectedIndices.Count = 1 Then
            '无选择项，有选中项
            MoveListViewItem(LV, LV.SelectedItems.Item(0).Index, True)
        End If



        LV.EndUpdate()
    End Sub


    ' 將選取的項目往下移動一個位置
    Private Sub ListView_MoveDown(ByVal LV As ListView)
        LV.BeginUpdate()
        '檢查有沒有選取項目
        If LV.CheckedIndices.Count > 0 Then
            '用for迴圈由大到小去巡覽
            For i As Integer = LV.CheckedIndices.Count - 1 To 0 Step -1
                Dim index As Integer = LV.CheckedIndices(i)
                '如果index為最後一項就不需要下移
                If index < ListView1.Items.Count - 1 Then
                    '如果index+1被選取就不進行下移
                    If LV.CheckedIndices.Contains(index + 1) Then
                        Continue For
                    End If
                    '進行換位置的動作
                    MoveListViewItem(LV, index, False)
                End If
            Next
        ElseIf LV.SelectedIndices.Count = 1 Then
            '无选择项，有选中项
            MoveListViewItem(LV, LV.SelectedItems.Item(0).Index, False)
        End If
        LV.EndUpdate()
    End Sub

    '交换listitem位置
    Private Sub MoveListViewItem(ByRef lv As ListView, ByVal selIdx As Integer, ByVal moveUp As Boolean)
        Dim i As Integer
        Dim cache As String

        With lv
            If moveUp Then
                ' ignore moveup of row(0)
                If selIdx = 0 Then
                    Exit Sub
                End If
                ' move the subitems for the previous row
                ' to cache so we can move the selected row up

                cache = .Items(selIdx - 1).Checked
                .Items(selIdx - 1).Checked = .Items(selIdx).Checked
                .Items(selIdx).Checked = cache

                cache = .Items(selIdx - 1).Selected
                .Items(selIdx - 1).Selected = .Items(selIdx).Selected
                .Items(selIdx).Selected = cache

                cache = .Items(selIdx - 1).Text.ToString
                .Items(selIdx - 1).Text = .Items(selIdx).Text
                .Items(selIdx).Text = cache

                For i = 0 To lv.Items(selIdx).SubItems.Count - 1
                    cache = .Items(selIdx - 1).SubItems(i).Text
                    .Items(selIdx - 1).SubItems(i).Text = .Items(selIdx).SubItems(i).Text
                    .Items(selIdx).SubItems(i).Text = cache
                Next


                '.Items(selIdx - 1).Selected = True
                '.Refresh()
                '.Focus()
            Else
                ' ignore move down of last row
                If selIdx = .Items.Count - 1 Then
                    Exit Sub
                End If
                ' move the subitems for the next row
                ' to cache so we can move the selected row down

                cache = .Items(selIdx + 1).Checked
                .Items(selIdx + 1).Checked = .Items(selIdx).Checked
                .Items(selIdx).Checked = cache

                cache = .Items(selIdx + 1).Selected
                .Items(selIdx + 1).Selected = .Items(selIdx).Selected
                .Items(selIdx).Selected = cache

                cache = .Items(selIdx + 1).Text
                .Items(selIdx + 1).Text = .Items(selIdx).Text
                .Items(selIdx).Text = cache

                For i = 0 To lv.Items(selIdx).SubItems.Count - 1
                    cache = .Items(selIdx + 1).SubItems(i).Text
                    .Items(selIdx + 1).SubItems(i).Text = .Items(selIdx).SubItems(i).Text
                    .Items(selIdx).SubItems(i).Text = cache
                Next

                '.Items(selIdx + 1).Selected = True
                '.Refresh()
                '.Focus()
            End If
        End With
    End Sub

    '重建最近的文件夹菜单
    Private Sub ReBuildRecentFolder()
        For i = 0 To Menu_Recent_Folder.DropDownItems.Count - 1
            Menu_Recent_Folder.DropDownItems.Item(i).Visible = False
        Next

        For i = 0 To ListBox1.Items.Count - 1
            If IsDirectoryExists(ListBox1.Items(i).ToString) = True Then
                Menu_Recent_Folder.DropDownItems.Item(i).Text = ListBox1.Items.Item(i)
                Menu_Recent_Folder.DropDownItems.Item(i).Visible = True
            End If
        Next

    End Sub

    '加载最近文件夹
    Private Sub LoadRecentFolder(ByVal FolderFullName As String)

        ToolStripStatusLabel1.Text = "扫描文件夹：" & FolderFullName
        Application.DoEvents()
        WaitStart()

        '扫描文件夹
        If DoModel = DRW Then

            'ListView1.Items.Clear()

            ListView1.BeginUpdate()

            TransFiles(FolderFullName, DoModel, ListView1)

            UnReg()

            ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)
            ListView1.Columns(0).Width = 40
            If ListView1.Columns(1).Width < (ListView1.Width - 100) Then
                ListView1.Columns(1).Width = ListView1.Width - 100
            End If

            ReBuiltNumber(ListView1)
            Set_LV_Color(ListView1, Color.LightBlue, Color.White)

            ListView1.EndUpdate()
            '-------------------------------------------------------------------------------------
        ElseIf DoModel = PRT Then
            'TreeView1.Nodes.Clear()
            'TreeView1.BeginUpdate()

            TransFiles(FolderFullName, DoModel, TreeView1)

            UnReg()
            'TreeView1.EndUpdate()
        End If

        WaitEnd()

        ToolStripStatusLabel1.Text = "扫描文件夹完成"

    End Sub

    '菜单-最近
    Private Sub Menu_RF1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_RF1.Click
        LoadRecentFolder(Menu_RF1.Text.ToString)
    End Sub

    Private Sub Menu_RF2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_RF2.Click
        LoadRecentFolder(Menu_RF2.Text.ToString)
    End Sub

    Private Sub Menu_RF3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_RF3.Click
        LoadRecentFolder(Menu_RF3.Text.ToString)
    End Sub

    Private Sub Menu_RF4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_RF4.Click
        LoadRecentFolder(Menu_RF4.Text.ToString)
    End Sub

    Private Sub Menu_RF5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_RF5.Click
        LoadRecentFolder(Menu_RF5.Text.ToString)
    End Sub

    '列表菜单--移除文件
    Private Sub TSMenu_ReMove_List_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMenu_ReMove_List.Click
        Menu_ReMove_List_Click(sender, e)
    End Sub

    '菜单--删除未使用的文件
    Private Sub Menu_Remove_UnUsed_File_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_Remove_UnUsed_File.Click
        Dim Folder As String


        MsgBox("将移动没用的文件到 \unusedfiles 文件夹，特别注意单个零件，请谨慎！", MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, "移动除没有使用的文件")


        With FolderBrowserDialog1
            .Description = "选择含有SLDASM的文件夹"
            .ShowNewFolderButton = False
            .RootFolder = System.Environment.SpecialFolder.Desktop
            Dim result As DialogResult = .ShowDialog()
            If result = DialogResult.OK Then
                Folder = .SelectedPath
            Else
                Exit Sub
            End If
        End With

        ToolStripStatusLabel1.Text = "扫描文件夹：" & Folder
        Application.DoEvents()
        WaitStart()

        ListBox1.Items.Clear()
        ListBox1.Visible = False
        ListBox1.Sorted = True

        '遍历使用到的sld文件
        TransFiles(Folder, ASM, ListBox1)

        ToolStripStatusLabel1.Text = "对比并移动文件"

        '移动删除文件 
        ReMoveUnusedFile(Folder, ListBox1)

        WaitEnd()

        ToolStripStatusLabel1.Text = "移动未使用的文件完成"
        MsgBox("移动未使用的文件完成", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "移动未使用的文件")

        '打开扫面的文件夹
        System.Diagnostics.Process.Start(Folder)

    End Sub

    '菜单--复制零件
    Private Sub Menu_Saveas_Prt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_Clone_Prt.Click
        Dim Source_SldPrt_File As String
        Dim Source_SldDrw_File As String

        Dim Destination_SldPrt_File As String
        Dim Destination_SldDrw_File As String

        With OpenFileDialog1
            .Multiselect = False
            .Title = "原零件文件"
            .Filter = "SW零件(*.SLDPRT)|*.SLDPRT"
            .CheckFileExists = False
            If .ShowDialog = DialogResult.OK Then
                Source_SldPrt_File = .FileName
            Else
                Exit Sub
            End If
        End With

        With SaveFileDialog1
            .Title = "复制零件文件为"
            .Filter = "SW零件(*.SLDPRT)|*.SLDPRT"
            .CheckFileExists = False
            If .ShowDialog = DialogResult.OK Then
                Destination_SldPrt_File = .FileName
            Else
                Exit Sub
            End If
        End With

        If IsFileExsts(Destination_SldPrt_File) = True Then
            If MsgBox("文件  " & Destination_SldPrt_File & " 存在，是否覆盖？", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "复制零件") = MsgBoxResult.No Then
                Exit Sub
            End If
        End If

        Dim swApp As SldWorks
        swApp = CreateObject("SldWorks.Application")

        '复制零件
        swApp.CopyDocument(Source_SldPrt_File, Destination_SldPrt_File, "", "", swMoveCopyOptions_e.swMoveCopyOptionsOverwriteExistingDocs)
        'My.Computer.FileSystem.CopyFile(Source_SldPrt_File, Destination_SldPrt_File, True)

        '获取工程图
        Source_SldDrw_File = ChangeFileExe(Source_SldPrt_File, DRW)
        Destination_SldDrw_File = ChangeFileExe(Destination_SldPrt_File, DRW)

        '复制工程图
        swApp.CopyDocument(Source_SldDrw_File, Destination_SldDrw_File, "", "", swMoveCopyOptions_e.swMoveCopyOptionsOverwriteExistingDocs)
        'My.Computer.FileSystem.CopyFile(Source_SldDrw_File, Destination_SldDrw_File, True)

        '更改工程图的参考关系
        swApp.ReplaceReferencedDocument(Destination_SldDrw_File, Source_SldPrt_File, Destination_SldPrt_File)

        MsgBox("复制零件完成", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "复制零件")


    End Sub

    '菜单---检查更新
    Private Sub Menu_Update_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_Update.Click
        'Dim LocalFolder As String
        'Dim LocalFile As String

        'Dim OldVer As String
        'Dim Url As String



        'Me.Cursor = Cursors.WaitCursor

        'OldVer = My.Application.Info.Version.ToString

        'LocalFolder = My.Application.Info.DirectoryPath
        'LocalFile = LocalFolder & "\SwTool.ini"

        'Dim Ini As New Ini
        'Url = Ini.INIRead(LocalFile, "SwTool", "Url")
        'If Url = "" Then
        '    Url = UpdateiniUrl
        'End If

        'If IsFileExsts(LocalFolder & "\simpleupdater.exe") Then
        '    Process.Start(LocalFolder & "\simpleupdater.exe", "-option=copyself  -ver=" & OldVer & " -url=" & Url & " -local=" & LocalFolder & " -start=swtool.exe", "", Nothing, "")
        'Else
        '    MsgBox("升级程序错误，转到googlecode + 百度盘下载...", MsgBoxStyle.Information)
        '    Process.Start(GoogleCode)
        Process.Start(BaiDuPan)
        'End If

        'Me.Cursor = Cursors.Default

    End Sub

    '菜单--复制装配
    Private Sub Menu_Clone_ASM_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_Clone_ASM.Click
        Dim Source_SLdPrt_Folder As String
        Dim Source_SldPrt_File As String
        Dim Source_SldDrw_File As String

        Dim Destination_SLdPrt_Folder As String
        Dim Destination_SldPrt_File As String
        Dim Destination_SldDrw_File As String

        With OpenFileDialog1
            .Multiselect = False
            .Title = "原装配体文件"
            .Filter = "SW装配体(*.SLDASM)|*.SLDASM"
            .CheckFileExists = True
            If .ShowDialog = DialogResult.OK Then
                Source_SldPrt_File = .FileName
                Source_SLdPrt_Folder = GetFileNameInfo(Source_SldPrt_File).Folder
            Else
                Exit Sub
            End If
        End With


        'With FolderBrowserDialog1
        '    .Description = "复制装配体到"
        '    .ShowNewFolderButton = True
        '    .RootFolder = System.Environment.SpecialFolder.Desktop
        '    Dim result As DialogResult = .ShowDialog()
        '    If result = DialogResult.OK Then
        '        Destination_SLdPrt_Folder = .SelectedPath
        '    Else
        '        Exit Sub
        '    End If
        'End With

        With SaveFileDialog1
            .Title = "复制装配体文件为"
            .Filter = "SW装配体(*.SLDASM)|*.SLDASM"
            .CheckFileExists = False
            If .ShowDialog = DialogResult.OK Then
                Destination_SldPrt_File = .FileName
                Destination_SLdPrt_Folder = GetFileNameInfo(Destination_SldPrt_File).Folder
            Else
                Exit Sub
            End If
        End With

        'If IsFileExsts(Destination_SLdPrt_Folder) = False Then

        'End If

        Me.Cursor = Cursors.WaitCursor
        Application.DoEvents()
        WaitStart()

        'ListBox1.Items.Clear()
        ListBox1.Visible = False


        '遍历sld文件
        'ListBox1.Items.Add(Source_SldPrt_File)

        Dim vDepend As Object
        Dim SubFullName As String
        Dim SubName As String

        Dim sourcefiles() As String
        Dim targetfiles() As String

        Dim swApp As SldWorks
        swApp = CreateObject("SldWorks.Application")
        swApp.Visible = True

        vDepend = Nothing
        vDepend = swApp.GetDocumentDependencies2(Source_SldPrt_File, True, True, False)
        If vDepend Is Nothing = True Then Exit Sub

        ReDim sourcefiles((UBound(vDepend) - 1) / 2)
        ReDim targetfiles((UBound(vDepend) - 1) / 2)

        For Index = 0 To (UBound(vDepend) - 1) / 2
            SubName = vDepend(2 * Index)   '简名
            SubFullName = vDepend(2 * Index + 1)  '全名

            sourcefiles(Index) = SubFullName
            targetfiles(Index) = Replace(SubFullName, GetFileNameInfo(SubFullName).Folder, Destination_SLdPrt_Folder)

            'If IsSWFile(SubFullName, ASM & PRT) = True Then
            '    ListBox1.Items.Add(SubFullName)
            'End If

        Next


        '目标文件
        'Destination_SldPrt_File = Replace(Source_SldPrt_File, GetFileNameInfo(Source_SldPrt_File).Folder, Destination_SLdPrt_Folder)
        '复制装配体和零件
        swApp.CopyDocument(Source_SldPrt_File, Destination_SldPrt_File, sourcefiles, targetfiles, swMoveCopyOptions_e.swMoveCopyOptionsOverwriteExistingDocs)
        '获取装配体工程图
        Source_SldDrw_File = ChangeFileExe(Source_SldPrt_File, DRW)
        Destination_SldDrw_File = ChangeFileExe(Destination_SldPrt_File, DRW)
        '复制工程图
        swApp.CopyDocument(Source_SldDrw_File, Destination_SldDrw_File, "", "", swMoveCopyOptions_e.swMoveCopyOptionsOverwriteExistingDocs)
        '更改工程图的参考关系
        swApp.ReplaceReferencedDocument(Destination_SldDrw_File, Source_SldPrt_File, Destination_SldPrt_File)


        For index = 0 To UBound(sourcefiles)
            '获取工程图
            Source_SldPrt_File = sourcefiles(index)
            Source_SldDrw_File = ChangeFileExe(Source_SldPrt_File, DRW)
            Destination_SldPrt_File = targetfiles(index)
            Destination_SldDrw_File = ChangeFileExe(Destination_SldPrt_File, DRW)
            '复制工程图
            swApp.CopyDocument(Source_SldDrw_File, Destination_SldDrw_File, "", "", swMoveCopyOptions_e.swMoveCopyOptionsOverwriteExistingDocs)
            '更改工程图的参考关系
            swApp.ReplaceReferencedDocument(Destination_SldDrw_File, Source_SldPrt_File, Destination_SldPrt_File)
        Next

        WaitEnd()

        MsgBox("复制装配体完成", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "复制装配体")

    End Sub

    '菜单--替换图层
    Private Sub Menu_Layer_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_Layer.Click

        Dim LayerFileFullName As String
        Dim swApp As SldWorks

        swApp = CreateObject("SldWorks.Application")

        With OpenFileDialog1
            .Reset()
            .Multiselect = False
            .InitialDirectory = GetParentFolder(swApp.GetUserPreferenceStringValue(swDefaultTemplateDrawing))
            .Title = "打开 DrwDot,Slddrw 文件"
            .Filter = "SW工程图模板(*.drwdot)|*.drwdot|SW工程图(*.slddrw)|*.slddrw"
            Dim result As DialogResult = .ShowDialog()
            If result = DialogResult.OK Then
                LayerFileFullName = .FileName
            Else
                Exit Sub
            End If
        End With

        Dim LayerArr() As LayerA = GeyLayers(LayerFileFullName)


        Me.Cursor = Cursors.WaitCursor
        If ListView1.CheckedItems.Count > 0 Then
            With ToolStripProgressBar1
                .Minimum = 0
                .Maximum = ListView1.CheckedItems.Count
                .Value = 0
            End With

            For Each LVI In ListView1.CheckedItems
                '添加任务代码
                '--------------------------------
                Application.DoEvents()
                A_SLDDRWFile.Slddrw_File_Name = LVI.SubItems(1).Text

                If IsFileExsts(A_SLDDRWFile.Slddrw_File_Name) = True Then
                    ToolStripStatusLabel1.Text = "进程文件：" & A_SLDDRWFile.Slddrw_File_Name
                    '----------------------------------
                    SetLayer(A_SLDDRWFile.Slddrw_File_Name, LayerArr)

                    '--------------------------------
                End If

                '----------------------------------

                ToolStripProgressBar1.Value = ToolStripProgressBar1.Value + 1
                ToolStripStatusLabel2.Text = Val(ToolStripProgressBar1.Value.ToString) * 100 \ ToolStripProgressBar1.Maximum & "%"

            Next

        End If
        ToolStripStatusLabel1.Text = "替换图层完成"
        PrintDocument1.Dispose()
        Me.Cursor = Cursors.Default
        MsgBox("替换图层完成", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "替换图层")

    End Sub

    '菜单--复制工程图
    Private Sub Menu_Clone_Drw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_Clone_Drw.Click
        Dim Source_SldPrt_File As String
        Dim Source_SldDrw_File As String

        Dim Destination_SldPrt_File As String
        Dim Destination_SldDrw_File As String

        '获取源工程图
        With OpenFileDialog1
            .Multiselect = False
            .Title = "原工程图文件"
            .Filter = "SW工程图(*.SLDDRW)|*.SLDDRW"
            .CheckFileExists = False
            If .ShowDialog = DialogResult.OK Then
                Source_SldDrw_File = .FileName
            Else
                Exit Sub
            End If
        End With

        '获取源零件
        Source_SldPrt_File = ChangeFileExe(Source_SldDrw_File, PRT)
        If IsFileExsts(Source_SldPrt_File) = False Then
            Source_SldPrt_File = ChangeFileExe(Source_SldPrt_File, ASM)
        End If

        If IsFileExsts(Source_SldPrt_File) = False Then
            MsgBox("没有找到源工程图对应的装配体或者零件，将不能链接关系！", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "复制工程图")
        End If



        '获取目标工程图或者零件
        With SaveFileDialog1
            .Title = "复制工程图文件为"
            .Filter = "SW文件|*.SLDASM;*.SLDPRT;*.SLDDRW"
            .CheckFileExists = False
            If .ShowDialog = DialogResult.OK Then
                Destination_SldDrw_File = .FileName
            Else
                Exit Sub
            End If
        End With

        Destination_SldPrt_File = Nothing

        '判断目标文件类型
        Select Case GetFileExtension(Destination_SldDrw_File)
            Case PRT, ASM       '零件，获取目标工程图
                Destination_SldPrt_File = Destination_SldDrw_File
                Destination_SldDrw_File = ChangeFileExe(Destination_SldPrt_File, DRW)

            Case DRW        '工程图，获取目标零件
                If IsFileExsts(Destination_SldDrw_File) = True Then
                    If MsgBox("文件  " & Destination_SldDrw_File & " 存在，是否覆盖？", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "复制工程图") = MsgBoxResult.No Then
                        Exit Sub
                    End If
                End If
                Destination_SldPrt_File = ChangeFileExe(Destination_SldDrw_File, PRT)

                If IsFileExsts(Destination_SldPrt_File) = False Then
                    Destination_SldPrt_File = ChangeFileExe(Destination_SldPrt_File, ASM)
                End If

                If IsFileExsts(Destination_SldPrt_File) = False Then
                    MsgBox("没有找到目标工程图对应的装配体或者零件，将不能链接关系！", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "复制工程图")
                End If


        End Select


        Dim swApp As SldWorks
        swApp = CreateObject("SldWorks.Application")


        '复制工程图
        swApp.CopyDocument(Source_SldDrw_File, Destination_SldDrw_File, "", "", swMoveCopyOptions_e.swMoveCopyOptionsOverwriteExistingDocs)


        '更改工程图的参考关系
        swApp.ReplaceReferencedDocument(Destination_SldDrw_File, Source_SldPrt_File, Destination_SldPrt_File)

        MsgBox("复制工程图完成", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "复制工程图")

    End Sub

    '菜单--检查外部参考
    Private Sub TSMenu_CheckExtRefer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMenu_CheckExtRefer.Click
        On Error Resume Next
        Dim Node As TreeNode
        Dim FileFulName As String

        Application.DoEvents()


        Node = Me.TreeView1.SelectedNode
        FileFulName = Node.ToolTipText.ToString
        If IsSWFile(FileFulName, ASM) Then
            Me.Cursor = Cursors.WaitCursor
            WaitStart()

            '遍历指定node，返回外部参考和node比较，有不同的就提示
            TraverseNodeListExternalFileReference(Node)

            Me.Cursor = Cursors.Default
            WaitEnd()

            MsgBox("检查外部参考完成", MsgBoxStyle.Information, "检查外部参考")
        Else
            MsgBox("请选择一个装配文件。", MsgBoxStyle.Information, "检查外部参考")
        End If

        'vDependArr = GetExternalReference(FileFulName)
        'vDependArr = ListExternalFileReference(FileFulName)


    End Sub

    '菜单--移动装配
    Private Sub Menu_Move_Asm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_Move_Asm.Click

        Dim Source_SLdPrt_Folder As String
        Dim Source_SldPrt_File As String
        Dim Source_SldDrw_File As String

        Dim Destination_SLdPrt_Folder As String
        Dim Destination_SldPrt_File As String
        Dim Destination_SldDrw_File As String

        With OpenFileDialog1
            .Multiselect = False
            .Title = "原SLDASM文件"
            .Filter = "SW装配体(*.SLDASM)|*.SLDASM"
            .CheckFileExists = True
            If .ShowDialog = DialogResult.OK Then
                Source_SldPrt_File = .FileName
                Source_SLdPrt_Folder = GetFileNameInfo(Source_SldPrt_File).Folder
            Else
                Exit Sub
            End If
        End With


        'With FolderBrowserDialog1
        '    .Description = "移动装配体到"
        '    .ShowNewFolderButton = True
        '    .RootFolder = System.Environment.SpecialFolder.Desktop
        '    Dim result As DialogResult = .ShowDialog()
        '    If result = DialogResult.OK Then
        '        Destination_SLdPrt_Folder = .SelectedPath
        '    Else
        '        Exit Sub
        '    End If
        'End With

        With OpenFileDialog1
            .Multiselect = False
            .Title = "移动SLDASM文件"
            .Filter = "SW装配体(*.SLDASM)|*.SLDASM"
            .CheckFileExists = False
            If .ShowDialog = DialogResult.OK Then
                Destination_SldPrt_File = .FileName
                Destination_SLdPrt_Folder = GetFileNameInfo(Destination_SldPrt_File).Folder
            Else
                Exit Sub
            End If
        End With

        Me.Cursor = Cursors.WaitCursor
        Application.DoEvents()
        WaitStart()

        'ListBox1.Items.Clear()
        ListBox1.Visible = False


        '遍历sld文件
        'ListBox1.Items.Add(Source_SldPrt_File)

        Dim vDepend As Object
        Dim SubFullName As String
        Dim SubName As String

        Dim sourcefiles() As String
        Dim targetfiles() As String

        Dim swApp As SldWorks
        swApp = CreateObject("SldWorks.Application")
        swApp.Visible = True

        vDepend = Nothing
        vDepend = swApp.GetDocumentDependencies2(Source_SldPrt_File, True, True, False)
        If vDepend Is Nothing = True Then Exit Sub

        ReDim sourcefiles((UBound(vDepend) - 1) / 2)
        ReDim targetfiles((UBound(vDepend) - 1) / 2)

        For Index = 0 To (UBound(vDepend) - 1) / 2
            SubName = vDepend(2 * Index)   '简名
            SubFullName = vDepend(2 * Index + 1)  '全名

            sourcefiles(Index) = SubFullName
            targetfiles(Index) = Replace(SubFullName, Source_SLdPrt_Folder, Destination_SLdPrt_Folder)

            'If IsSWFile(SubFullName, ASM & PRT) = True Then
            '    ListBox1.Items.Add(SubFullName)
            'End If

        Next

        '目标文件
        'Destination_SldPrt_File = Replace(Source_SldPrt_File, GetFileNameInfo(Source_SldPrt_File).Folder, Destination_SLdPrt_Folder)
        '移动装配体和零件
        swApp.MoveDocument(Source_SldPrt_File, Destination_SldPrt_File, sourcefiles, targetfiles, swMoveCopyOptions_e.swMoveCopyOptionsOverwriteExistingDocs)
        '获取装配体工程图
        Source_SldDrw_File = ChangeFileExe(Source_SldPrt_File, DRW)
        Destination_SldDrw_File = ChangeFileExe(Destination_SldPrt_File, DRW)
        '移动工程图
        swApp.MoveDocument(Source_SldDrw_File, Destination_SldDrw_File, "", "", swMoveCopyOptions_e.swMoveCopyOptionsOverwriteExistingDocs)
        '更改工程图的参考关系
        swApp.ReplaceReferencedDocument(Destination_SldDrw_File, Source_SldPrt_File, Destination_SldPrt_File)


        For index = 0 To UBound(sourcefiles)
            '获取工程图
            Source_SldPrt_File = sourcefiles(index)
            Source_SldDrw_File = ChangeFileExe(Source_SldPrt_File, DRW)
            Destination_SldPrt_File = targetfiles(index)
            Destination_SldDrw_File = ChangeFileExe(Destination_SldPrt_File, DRW)
            '移动工程图
            swApp.MoveDocument(Source_SldDrw_File, Destination_SldDrw_File, "", "", swMoveCopyOptions_e.swMoveCopyOptionsOverwriteExistingDocs)
            '更改工程图的参考关系
            swApp.ReplaceReferencedDocument(Destination_SldDrw_File, Source_SldPrt_File, Destination_SldPrt_File)
        Next

        WaitEnd()

        MsgBox("移动装配体完成", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "移动装配体")

    End Sub


    Private Sub 测试ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 测试ToolStripMenuItem.Click

        MsgBox("测试用")
        '打包装配体
        '-----------------------------------------------------------
        'Dim swModelDoc As ModelDoc2
        'Dim swModelDocExt As ModelDocExtension
        'Dim swPackAndGo As PackAndGo
        'Dim openFile As String
        'Dim myFileName As String
        'Dim pgFileNames As Object = Nothing
        'Dim pgFileStatus As Object = Nothing
        'Dim pgSetFileNames() As String
        'Dim pgGetFileNames As Object
        'Dim pgDocumentStatus As Object
        'Dim status As Boolean
        'Dim warnings As Integer
        'Dim errors As Integer
        'Dim i As Integer
        'Dim j As Integer
        'Dim namesCount As Integer
        'Dim myPath As String
        'Dim statuses As Object

        'Dim swApp As SldWorks
        'swApp = CreateObject("SldWorks.Application")
        'swApp.Visible = True


        '' Open assembly
        'openFile = "C:\Program Files\SolidWorks Corp\SolidWorks\samples\tutorial\advdrawings\handle.sldasm"
        'swModelDoc = swApp.OpenDoc6(openFile, swDocumentTypes_e.swDocASSEMBLY, swOpenDocOptions_e.swOpenDocOptions_Silent, "", errors, warnings)
        'swModelDocExt = swModelDoc.Extension

        '' Get Pack and Go object
        'Debug.Print("Pack and Go")
        'swPackAndGo = swModelDocExt.GetPackAndGo

        '' Get number of documents in assembly
        'namesCount = swPackAndGo.GetDocumentNamesCount
        'Debug.Print("  Number of model documents: " & namesCount)

        '' Get whether to include any drawings and simulation results
        'Debug.Print("  Include drawings: " & swPackAndGo.IncludeDrawings)
        'Debug.Print("  Include simulation results: " & swPackAndGo.IncludeSimulationResults)

        '' Get current paths and filenames of the assembly's documents
        'status = swPackAndGo.GetDocumentNames(pgFileNames)
        'Debug.Print("")
        'Debug.Print("  Current path and filenames: ")
        'If Not pgFileNames Is Nothing Then
        '    For i = 0 To UBound(pgFileNames)
        '        Debug.Print("    The path and filename is: " & pgFileNames(i))
        '    Next i
        'End If

        '' Get current save-to paths and filenames of the assembly's documents
        'status = swPackAndGo.GetDocumentSaveToNames(pgFileNames, pgFileStatus)
        'Debug.Print("")
        'Debug.Print("  Current default save-to filenames: ")
        'If Not pgFileNames Is Nothing Then
        '    For i = 0 To UBound(pgFileNames)
        '        Debug.Print("   The path and filename is: " & pgFileNames(i))
        '    Next i
        'End If

        '' Folder where to save the files
        'myPath = "C:\temp\"

        '' Create your own filenames for the model's documents
        'ReDim pgSetFileNames(namesCount - 1)
        'Debug.Print("")
        'Debug.Print("  My Pack and Go path and filenames before adding prefix and suffix: ")
        'j = 0
        'For i = 0 To (namesCount - 1)
        '    myFileName = pgFileNames(i)
        '    ' Determine type of SolidWorks file based on file extension
        '    If InStr(LCase(myFileName), "sldprt") > 0 Then
        '        myFileName = j & ".sldprt"
        '    ElseIf InStr(LCase(myFileName), "sldasm") > 0 Then
        '        myFileName = j & ".sldasm"
        '    ElseIf InStr(LCase(myFileName), "slddrw") > 0 Then
        '        myFileName = j & ".slddrw"
        '    Else
        '        ' Only packing up SolidWorks files
        '        Exit Sub
        '    End If
        '    pgSetFileNames(i) = myPath & myFileName
        '    Debug.Print("    My path and filename is: " & pgSetFileNames(i))
        '    j = j + 1
        'Next i

        '' Set document paths and filenames for Pack and Go
        'status = swPackAndGo.SetDocumentSaveToNames(pgSetFileNames)

        '' Add a prefix and suffix to the filenames
        'swPackAndGo.AddPrefix = "SW"
        'swPackAndGo.AddSuffix = "PackAndGo"

        '' Verify document paths and filenames after adding prefix and suffix
        'ReDim pgGetFileNames(namesCount - 1)
        'ReDim pgDocumentStatus(namesCount - 1)
        'status = swPackAndGo.GetDocumentSaveToNames(pgGetFileNames, pgDocumentStatus)
        'Debug.Print("")
        'Debug.Print("  My Pack and Go path and filenames after adding prefix and suffix: ")
        'For i = 0 To (namesCount - 1)
        '    Debug.Print("    My path and filename is: " & pgGetFileNames(i))
        'Next i


        '' Pack and Go
        'statuses = swModelDocExt.SavePackAndGo(swPackAndGo)
        '-------------------------------------------------------------------------------------------------------

    End Sub


    '添加到收藏夹
    Private Sub Menu_AddTo_Favor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_AddTo_Favor.Click
        Dim FileFullName As String = Nothing
        Dim ParentFolder As String = Nothing

        '获取文件夹
        Select Case DoModel
            Case DRW
                If ListView1.SelectedItems.Count > 0 Then
                    FileFullName = ListView1.SelectedItems(0).SubItems(1).Text.ToString
                End If
            Case PRT
                If TreeView1.Nodes.Count > 0 Then
                    FileFullName = TreeView1.SelectedNode.ToolTipText
                End If
        End Select

        If IsFileExsts(FileFullName) = False Then
            Exit Sub
        End If

        ParentFolder = GetParentFolder(FileFullName)

        '检查是否重复
        For Each amenustrip As ToolStripMenuItem In Menu_Favor.DropDownItems
            If ParentFolder = amenustrip.Text.ToString Then
                Exit Sub
            End If
        Next

        '添加到收藏夹
        Dim doItem As ToolStripMenuItem = MenuStrip1.Items("Menu_Favor")
        Dim newItem As ToolStripMenuItem = doItem.DropDownItems.Add(ParentFolder, Nothing, AddressOf menuItem_Click)
        newItem.Name = ParentFolder
        
    End Sub

    '单击一个收藏
    Private Sub menuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Menu_Favor.Click
        On Error Resume Next
        Dim ParentFolder As String

        ''sender就是你点击的菜单项，转换即可。    
        Dim m As ToolStripMenuItem = CType(sender, ToolStripMenuItem)

        ParentFolder = m.Name
        
        Select Case Menu_Edit_Favor.CheckState
            Case CheckState.Checked
                If m.Name <> "Menu_Favor" Then
                    Menu_Favor.DropDownItems.Remove(m)
                End If
            Case CheckState.Unchecked
                If IsDirectoryExists(ParentFolder) Then
                    System.Diagnostics.Process.Start(ParentFolder)
                End If
        End Select

    End Sub


    '整理收藏夹, 选择状态时单击收藏将删除

    Private Sub Menu_Edit_Favor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_Edit_Favor.Click
        Select Case sender.checkstate
            Case CheckState.Checked
                Menu_Edit_Favor.CheckState = CheckState.Unchecked
            Case CheckState.Unchecked
                Menu_Edit_Favor.CheckState = CheckState.Checked
                MsgBox("收藏夹进入整理模式，单击删除。", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "整理模式")
        End Select

    End Sub

End Class

